close
The Wayback Machine - https://web.archive.org/web/20240818163943/https://www.geeksforgeeks.org/c-program-cyclically-rotate-array-one/
Open In App

Program to cyclically rotate an array by one

Last Updated : 11 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given an array, the task is to cyclically rotate the array clockwise by one time. 

Examples:  

Input: arr[] = {1, 2, 3, 4, 5}
Output: arr[] = {5, 1, 2, 3, 4}

Input: arr[] = {2, 3, 4, 5, 1}
Output: {1, 2, 3, 4, 5}

Assign every element with its previous element and first element with the last element .

Illustrations:

Consider an array: arr[] = {1, 2, 3, 4, 5}

  • Initialize last element in variable ‘last_el’ that is 5
  • Then, iterate from n-1 to 1 and assign arr[i] = arr[i-1]
    • arr[4] = arr[3]
      • arr[ ] = {1, 2, 3, 4, 4}
    • arr[3] = arr[2]
      • arr[ ] = {1, 2, 3, 3, 4}
    • arr[2] = arr[1]
      • arr[ ] = {1, 2, 2, 3, 4}
    • arr[1] = arr[0]
      • arr[ ] = {1, 1, 2, 3, 4}
  • Assign arr[0] = last_el
    • arr[0] = 5
      • arr[ ] = {5, 1, 2, 3, 4}
  • Thus the required array will be {5, 1, 2, 3, 4}

Follow the steps to solve the problem:

  • Store the last element in any temp variable
  • Shift every element one position ahead
  • Assign first value = last value (stored in temp variable).

Below is the implementation for the above approach:

C++14




// C++ code for program
// to cyclically rotate
// an array by one
 
#include <iostream>
using namespace std;
 
void rotate(int arr[], int n)
{
 
    // store the last element in a variable
    int last_el = arr[n - 1];
 
    // assign every value by its predecessor
    for (int i = n - 1; i > 0; i--) {
        arr[i] = arr[i - 1];
    }
 
    // first element will be assigned by last element
    arr[0] = last_el;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Printing initial array
    cout << "Given array is \n";
    for (int i = 0; i < n; i++)
        cout << arr[i] << ' ';
 
    // Calling rotate function
    rotate(arr, n);
 
    // Printing rotated array
    cout << "\nRotated array is\n";
    for (int i = 0; i < n; i++)
        cout << arr[i] << ' ';
 
    return 0;
}
 
// This code is contributed by jit_t


C




// C code for program
// to cyclically rotate
// an array by one
 
#include <stdio.h>
 
void rotate(int arr[], int n)
{
    // store the last element in a variable
    int last_el = arr[n - 1];
    for (int i = n - 1; i > 0; i--)
        arr[i] = arr[i - 1];
    // assign the last element to first element
    arr[0] = last_el;
}
 
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 }, i;
    int n = sizeof(arr) / sizeof(arr[0]);
 
    printf("Given array is\n");
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
 
    // Function Call
    rotate(arr, n);
 
    printf("\nRotated array is\n");
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
 
    return 0;
}


Java




// Java code for program
// to cyclically rotate
// an array by one
 
import java.util.Arrays;
 
public class Test {
    static int arr[] = new int[] { 1, 2, 3, 4, 5 };
 
    // Method for rotation
    static void rotate()
    {
        int last_el = arr[arr.length - 1], i;
        for (i = arr.length - 1; i > 0; i--)
            arr[i] = arr[i - 1];
        arr[0] = last_el;
    }
 
    /* Driver program */
    public static void main(String[] args)
    {
        System.out.println("Given Array is");
        System.out.println(Arrays.toString(arr));
 
        // Function Call
        rotate();
 
        System.out.println("Rotated Array is");
        System.out.println(Arrays.toString(arr));
    }
}


Python3




# Python3 code for program to
# cyclically rotate an array by one
 
# Method for rotation
 
 
def rotate(arr, n):
    last_el = arr[n - 1]
 
    for i in range(n - 1, 0, -1):
        arr[i] = arr[i - 1]
 
    arr[0] = last_el
 
 
# Driver function
arr = [1, 2, 3, 4, 5]
n = len(arr)
print("Given array is")
for i in range(0, n):
    print(arr[i], end=' ')
 
rotate(arr, n)
 
print("\nRotated array is")
for i in range(0, n):
    print(arr[i], end=' ')
 
# This article is contributed
# by saloni1297


C#




// C# code for program to cyclically
// rotate an array by one
using System;
 
public class Test {
    static int[] arr = new int[] { 1, 2, 3, 4, 5 };
 
    // Method for rotation
    static void rotate()
    {
        int last_el = arr[arr.Length - 1], i;
 
        for (i = arr.Length - 1; i > 0; i--)
            arr[i] = arr[i - 1];
 
        arr[0] = last_el;
    }
 
    // Driver Code
    public static void Main()
    {
        Console.WriteLine("Given Array is");
        string Original_array = string.Join(" ", arr);
        Console.WriteLine(Original_array);
 
        // Function Call
        rotate();
 
        Console.WriteLine("Rotated Array is");
        string Rotated_array = string.Join(" ", arr);
        Console.WriteLine(Rotated_array);
    }
}
 
// This code is contributed by vt_m.


Javascript




<script>
 
// JavaScript code for program
// to cyclically rotate
// an array by one
function rotate(arr, n)
{
  var last_el = arr[n-1], i;
  for(i = n-1; i > 0; i--)
      arr[i] = arr[i-1];
  arr[0] = last_el;   
}
 
var arr = [1, 2, 3, 4, 5];
var n = arr.length;
 
document.write("Given array is <br>");
for(var i = 0; i< n; i++)
    document.write(arr[i] + " ");
     
rotate(arr, n);
 
document.write("<br>Rotated array is <br>");
for(var i = 0; i < n; i++)
    document.write(arr[i] + " ");
     
</script>


PHP




<?php
// PHP code for program
// to cyclically rotate
// an array by one
 
function rotate(&$arr, $n)
{
    $last_el = $arr[$n - 1];
    for ($i = $n - 1;
         $i > 0; $i--)
    $arr[$i] = $arr[$i - 1];
    $arr[0] = $last_el;
}
 
// Driver code
$arr = array(1, 2, 3, 4, 5);
$n = sizeof($arr);
 
echo "Given array is \n";
for ($i = 0; $i < $n; $i++)
    echo $arr[$i] . " ";
 
rotate($arr, $n);
 
echo "\nRotated array is\n";
for ($i = 0; $i < $n; $i++)
    echo $arr[$i] . " ";
 
// This code is contributed
// by ChitraNayal
?>


Output

Given array is 
1 2 3 4 5 
Rotated array is
5 1 2 3 4 

Time Complexity: O(n), as we need to iterate through all the elements. Where n is the number of elements in the array.
Auxiliary Space: O(1), as we are using constant space.

Approach 2: To solve the problem follow the below idea:

We can use two pointers, As we know in cyclic rotation we will bring last element to first and shift rest in forward direction, we can do this by swapping every element with last element till we get to the last point.

Follow the steps to solve the problem:

  • Take two pointers i and j which point to first and last element of array respectively.
  • Start swapping arr[i] and arr[j] and keep j fixed and i moving towards j.
  • Repeat above step till i is not equal to j.

Below is the implementation for the above idea:

C++14




// C++ code for the above approach:
#include <iostream>
using namespace std;
 
void rotate(int arr[], int n)
{
 
    // i and j pointing to first and last
    // element respectively
    int i = 0, j = n - 1;
    while (i != j) {
        swap(arr[i], arr[j]);
        i++;
    }
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 }, i;
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << "Given array is \n";
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
 
    rotate(arr, n);
 
    cout << "\nRotated array is\n";
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
 
    return 0;
}


C




#include <stdio.h>
 
void swap(int* x, int* y)
{
    int temp = *x;
    *x = *y;
    *y = temp;
}
void rotate(int arr[], int n)
{
    int i = 0, j = n - 1;
    while (i != j) {
        swap(&arr;[i], &arr;[j]);
        i++;
    }
}
 
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 }, i;
    int n = sizeof(arr) / sizeof(arr[0]);
 
    printf("Given array is\n");
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
 
    rotate(arr, n);
 
    printf("\nRotated array is\n");
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
 
    return 0;
}


Java




import java.util.Arrays;
 
public class Test {
    static int arr[] = new int[] { 1, 2, 3, 4, 5 };
 
    static void rotate()
    {
        int i = 0, j = arr.length - 1;
        while (i != j) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            i++;
        }
    }
 
    /* Driver program */
    public static void main(String[] args)
    {
        System.out.println("Given Array is");
        System.out.println(Arrays.toString(arr));
 
        rotate();
 
        System.out.println("Rotated Array is");
        System.out.println(Arrays.toString(arr));
    }
}


Python3




def rotate(arr, n):
    i = 0
    j = n - 1
    while i != j:
      arr[i], arr[j] = arr[j], arr[i]
      i = i + 1
    pass
 
 
# Driver function
arr= [1, 2, 3, 4, 5]
n = len(arr)
print ("Given array is")
for i in range(0, n):
    print (arr[i], end = ' ')
 
rotate(arr, n)
 
print ("\nRotated array is")
for i in range(0, n):
    print (arr[i], end = ' ')


C#




// C# code for program to cyclically
// rotate an array by one
using System;
 
class GFG {
 
    static int[] arr = new int[] { 1, 2, 3, 4, 5 };
 
    // Method for rotation
    static void rotate()
    {
        int n = arr[arr.Length - 1];
 
        // i and j pointing to first and
        // last element respectively
        int i = 0, j = n - 1;
        while (i != j) {
            {
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
            i++;
        }
    }
 
    // Driver Code
    public static void Main()
    {
        Console.WriteLine("Given Array is");
        string Original_array = string.Join(" ", arr);
        Console.WriteLine(Original_array);
 
        rotate();
 
        Console.WriteLine("Rotated Array is");
        string Rotated_array = string.Join(" ", arr);
        Console.WriteLine(Rotated_array);
    }
}
 
// This code is contributed by annianni


Javascript




<script>
// JavaScript code for program
// to cyclically rotate
// an array by one using pointers i,j
 
function rotate(arr, n){
    var i = 0
    var j = n-1
    while(i != j){
        let temp;
 
        temp = arr[i];
        arr[i] = arr[j];
        arr[j]= temp;
        i =i+1
    }
}
 
var arr = [1, 2, 3, 4, 5];
var n = arr.length;
 
document.write("Given array is <br>");
for(var i = 0; i< n; i++)
    document.write(arr[i] + " ");
     
rotate(arr, n);
 
document.write("<br>Rotated array is <br>");
for(var i = 0; i < n; i++)
    document.write(arr[i] + " ");
</script>


Output

Given array is 
1 2 3 4 5 
Rotated array is
5 1 2 3 4 

Time Complexity: O(n), as we need to iterate through all the elements. Where n is the number of elements in the array.
Auxiliary Space: O(1), as we are using constant space.

Approach 3: To solve the problem follow the below idea:

We can use Reversal Algorithm also , reverse first n-1 elements and then whole array which will result into one right rotation.

Follow the steps to solve the problem:

  • Reverse the array two times.
  • First time we will reverse the first n-1(n=size of array) elements.
  • Finally, we will get our rotated array by reversing the entire array.

Below is the implementation for the above idea:

C++14




// C++ program to cyclically rotate an array by one
#include <iostream>
using namespace std;
 
void rotate(int arr[], int n, int k)
{
    // Reverse the first n-1 terms
    int i, j;
    for (i = 0, j = n - k - 1; i < j; i++, j--) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
    // Reverse the entire array
    for (i = 0, j = n - 1; i < j; i++, j--) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
 
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 1; // No. of rotations
    int i, j;
 
    cout << "Given array is \n";
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
 
    rotate(arr, n, k);
 
    cout << "\nRotated array is\n";
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
 
    return 0;
}


Java




// Java program to cyclically rotate an array by one
import java.util.*;
 
class Main {
    public static void main(String[] args)
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        int n = arr.length;
        int k = 1; // No. of rotations
        int i, j;
        System.out.println("Given array is ");
        for (i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
 
        // Reverse the first n-1 terms
        for (i = 0, j = n - k - 1; i < j; i++, j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
 
        // Reverse the entire array
        for (i = 0, j = n - 1; i < j; i++, j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
 
        System.out.println("\nRotated array is");
        for (i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
}


Python3




arr = [1, 2, 3, 4, 5]
n = len(arr)
k = 1  # No. of rotations
i, j = 0, 0
 
print("Given array is")
for i in range(n):
    print(arr[i], end=" ")
 
# Reverse the first n-1 terms
for i, j in zip(range(0, (n-k)//2), range(n-k-1, (n-k)//2-1, -1)):
    temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp
 
# Reverse the entire array
for i, j in zip(range(0, n//2), range(n-1, n//2-1, -1)):
    temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp
 
print("\nRotated array is")
for i in range(n):
    print(arr[i], end=" ")


C#




using System;
 
class Program {
    static void Main(string[] args)
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        int n = arr.Length;
        int k = 1; // No. of rotations
        int i, j;
 
        Console.WriteLine("Given array is:");
        for (i = 0; i < n; i++)
            Console.Write(arr[i] + " ");
 
        // Reverse the first n-1 terms
        for (i = 0, j = n - k - 1; i < j; i++, j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
 
        // Reverse the entire array
        for (i = 0, j = n - 1; i < j; i++, j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
 
        Console.WriteLine("\nRotated array is:");
        for (i = 0; i < n; i++)
            Console.Write(arr[i] + " ");
 
        Console.ReadLine();
    }
}
// This code is contributed by Prajwal Kandekar


Javascript




let arr = [1, 2, 3, 4, 5];
let n = arr.length;
let k = 1; // No. of rotations
let i, j;
 
console.log("Given array is");
for (i = 0; i < n; i++) {
    process.stdout.write(arr[i] + " ");
}
 
// Reverse the first n-1 terms
for (i = 0, j = n - k - 1; i < j; i++, j--) {
    let temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}
// Reverse the entire array
for (i = 0, j = n - 1; i < j; i++, j--) {
    let temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}
 
console.log("\nRotated array is");
console.log();
for (i = 0; i < n; i++) {
    process.stdout.write(arr[i] + " ");
}


Output

Given array is 
1 2 3 4 5 
Rotated array is
5 1 2 3 4 

Time Complexity: O(n), as we are reversing the array. Where n is the number of elements in the array.
Auxiliary Space: O(1), as we are using constant space.



Similar Reads

Javascript Program for Program to cyclically rotate an array by one
Given an array, cyclically rotate the array clockwise by one. Examples: Input: arr[] = {1, 2, 3, 4, 5} Output: arr[] = {5, 1, 2, 3, 4}Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Following are steps. 1) Store last element in a variable say x. 2) Shift all elements one position ahead. 3) Replace first element o
2 min read
Minimum decrements to make an Array at most 0 such that all array elements are cyclically decremented after a number is reduced to 0
Given a circular array arr[] of N integers and an array cost[], the task is to calculate the minimum number of operations required to make all elements of the array equal to 0, where at each operation decrement the value of an index i by 1. If the value of an index becomes 0, decrement the value of arr[i+1] by cost[i], decrement the value of arr[i
6 min read
Minimize steps to make all Array elements equal by cyclically shifting a segment or replacing prefix by maximum
Given an array arr[] consisting of N positive integers, the task is to print the minimum number of steps required to make the array such that all elements are equal by performing the following operations on the array any number of times (possibly 0). Operation-1: Select any prefix arr[1…k] such that max (arr[1], arr[2], …, arr[k]) = arr[k] and set
7 min read
Python3 Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N
Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples:  Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6, 3, 8, 5, 2} Explanation: Even element = {2, 4, 6
2 min read
Javascript Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N
Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples:  Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6, 3, 8, 5, 2} Explanation: Even element = {2, 4, 6
2 min read
Javascript Program for Rotate the matrix right by K times
Given a matrix of size N*M, and a number K. We have to rotate the matrix K times to the right side. Examples: Input : N = 3, M = 3, K = 2 12 23 34 45 56 67 78 89 91 Output : 23 34 12 56 67 45 89 91 78 Input : N = 2, M = 2, K = 2 1 2 3 4 Output : 1 2 3 4 A simple yet effective approach is to consider each row of the matrix as an array and perform an
2 min read
Javascript Program for Rotate Doubly linked list by N nodes
Given a doubly linked list, rotate the linked list counter-clockwise by N nodes. Here N is a given positive integer and is smaller than the count of nodes in linked list. N = 2Rotated List: Examples: Input : a b c d e N = 2 Output : c d e a b Input : a b c d e f g h N = 4 Output : e f g h a b c d Asked in Amazon To rotate the Doubly linked list, we
4 min read
Python3 Program to Rotate the matrix right by K times
Given a matrix of size N*M, and a number K. We have to rotate the matrix K times to the right side. Examples: Input : N = 3, M = 3, K = 2 12 23 34 45 56 67 78 89 91 Output : 23 34 12 56 67 45 89 91 78 Input : N = 2, M = 2, K = 2 1 2 3 4 Output : 1 2 3 4 A simple yet effective approach is to consider each row of the matrix as an array and perform an
2 min read
Python3 Program to Rotate all Matrix elements except the diagonal K times by 90 degrees in clockwise direction
Given a square matrix mat[][] of dimension N and an integer K, the task is to rotate the matrix by 90 degrees K times without changing the position of the diagonal elements. Examples: Input: mat[][] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}, {21, 22, 23, 24, 25}}, K = 1Output: 1 16 11 6 5   22 7 12 9 2 23 18 1
4 min read
Python3 Program to Rotate digits of a given number by K
INTRODUCTION: One important point to consider when working with the algorithm to rotate the digits of a given number by k positions is the time complexity. If we were to implement this algorithm using the approach shown in the previous example, the time complexity would be O(n), where n is the number of digits in the input number. This is because t
4 min read
Javascript Program to Rotate digits of a given number by K
Given two integers N and K, the task is to rotate the digits of N by K. If K is a positive integer, left rotate its digits. Otherwise, right rotate its digits. Examples: Input: N = 12345, K = 2Output: 34512 Explanation: Left rotating N(= 12345) by K(= 2) modifies N to 34512. Therefore, the required output is 34512 Input: N = 12345, K = -3Output: 34
2 min read
Python3 Program for Rotate matrix by 45 degrees
Given a matrix mat[][] of size N*N, the task is to rotate the matrix by 45 degrees and print the matrix. Examples: Input: N = 6, mat[][] = {{3, 4, 5, 1, 5, 9, 5}, {6, 9, 8, 7, 2, 5, 2}, {1, 5, 9, 7, 5, 3, 2}, {4, 7, 8, 9, 3, 5, 2}, {4, 5, 2, 9, 5, 6, 2}, {4, 5, 7, 2, 9, 8, 3}}Output: 3 6 4 1 9 5 4 5 8 1 4 7 9 7 54 5 8 7 2 9 5 2 9 5 5 7 9 3 3 2 5 5
3 min read
Python3 Program to Rotate the sub-list of a linked list from position M to N to the right by K places
Given a linked list and two positions 'm' and 'n'. The task is to rotate the sublist from position m to n, to the right by k places. Examples: Input: list = 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6, m = 2, n = 5, k = 2 Output: 1-&gt;4-&gt;5-&gt;2-&gt;3-&gt;6 Rotate the sublist 2 3 4 5 towards right 2 times then the modified list are: 1 4 5 2 3 6 Input: list
4 min read
Python3 Program to Rotate Doubly linked list by N nodes
Given a doubly linked list, rotate the linked list counter-clockwise by N nodes. Here N is a given positive integer and is smaller than the count of nodes in linked list. N = 2Rotated List: Examples: Input : a b c d e N = 2 Output : c d e a b Input : a b c d e f g h N = 4 Output : e f g h a b c d Asked in Amazon To rotate the Doubly linked list, we
4 min read
Python3 Program To Rotate Linked List Block Wise
Given a Linked List of length n and block length k rotate in a circular manner towards right/left each block by a number d. If d is positive rotate towards right else rotate towards left. Examples: Input: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8-&gt;9-&gt;NULL, k = 3 d = 1 Output: 3-&gt;1-&gt;2-&gt;6-&gt;4-&gt;5-&gt;9-&gt;7-&gt;8-&gt;NULL Explan
3 min read
Javascript Program to Rotate Linked List block wise
Given a Linked List of length n and block length k rotate in a circular manner towards right/left each block by a number d. If d is positive rotate towards right else rotate towards left. Examples:  Input: 1->2->3->4->5->6->7->8->9->NULL, k = 3 d = 1 Output: 3->1->2->6->4->5->9->7->8->NULL Explanation: Here blocks of size 3 are rotated towards righ
4 min read
Javascript Program for Rotate a Matrix by 180 degree
Given a square matrix, the task is that we turn it by 180 degrees in an anti-clockwise direction without using any extra space.  Examples :  Input : 1 2 3 4 5 6 7 8 9 Output : 9 8 7 6 5 4 3 2 1 Input : 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 Output : 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 Method: 1 (Only prints rotated matrix) The solution of this problem is that
6 min read
Python3 Program to Rotate bits of a number
Bit Rotation: A rotation (or circular shift) is an operation similar to a shift except that the bits that fall off at one end are put back to the other end. In the left rotation, the bits that fall off at the left end are put back at the right end. In the right rotation, the bits that fall off at the right end are put back at the left end. INTRODUC
3 min read
Python3 Program to Inplace rotate square matrix by 90 degrees | Set 1
Given a square matrix, turn it by 90 degrees in anti-clockwise direction without using any extra space.Examples : Input: Matrix: 1 2 3 4 5 6 7 8 9 Output: 3 6 9 2 5 8 1 4 7 The given matrix is rotated by 90 degree in anti-clockwise direction. Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 4 8 12 16 3 7 11 15 2 6 10 14 1 5 9 13 The given matr
5 min read
Javascript Program to Inplace rotate square matrix by 90 degrees | Set 1
Given a square matrix, turn it by 90 degrees in anti-clockwise direction without using any extra space.Examples : Input: Matrix: 1 2 3 4 5 6 7 8 9 Output: 3 6 9 2 5 8 1 4 7 The given matrix is rotated by 90 degree in anti-clockwise direction. Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 4 8 12 16 3 7 11 15 2 6 10 14 1 5 9 13 The given matr
5 min read
Javascript Program to Rotate Matrix Elements
Given a matrix, clockwise rotate elements in it. Examples: Input 1 2 3 4 5 6 7 8 9 Output: 4 1 2 7 5 3 8 9 6 For 4*4 matrix Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. The idea is to use loops similar to the program f
5 min read
Right rotate given Array K times using Pointers
Given an array arr[] of size N and an integer K, the task is to right rotate the array K times. Examples: Input: arr[] = {1, 3, 5, 7, 9}, K = 2Output: 7 9 1 3 5Explanation: After 1st rotation - {9, 1, 3, 5, 7}After 2nd rotation - {7, 9, 1, 3, 5} Input: {1, 2, 3, 4, 5, 6}, K = 2Output: 5 6 1 2 3 4 Approach: The naive approach and approach based on r
6 min read
Check if end of a sorted Array can be reached by repeated jumps of one more, one less or same number of indices as previous jump
Given a sorted array arr[] of size N, the task is to check if it is possible to reach the end of the given array from arr[1] by jumping either arr[i] + k - 1, arr[i] + k, or arr[i] + k + 1 in each move, where k represents the number of indices jumped in the previous move. Consider K = 1 initially. If it is possible, then print "Yes". Otherwise, pri
11 min read
Maximize the minimum element of Array by reducing elements one by one
Given an array arr[] containing N integers. In each operation, a minimum integer is chosen from the array and deleted from the array after subtracting it from the remaining elements. The task is to find the maximum of minimum values of the array after any number of such operations. Examples: Input: arr[] = {-1, -2, 4, 3, 5}Output: 4Explanation: Fol
6 min read
Rotate a Matrix by 180 degree
Given a square matrix, the task is that we turn it by 180 degrees in an anti-clockwise direction without using any extra space. Examples : Input : 1 2 3 4 5 6 7 8 9 Output : 9 8 7 6 5 4 3 2 1 Input : 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 Output : 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 Method: 1 (Only prints rotated matrix) The solution of this problem is that t
15+ min read
String slicing in Python to Rotate a String
Given a string of size n, write functions to perform following operations on string. Left (Or anticlockwise) rotate the given string by d elements (where d &lt;= n).Right (Or clockwise) rotate the given string by d elements (where d &lt;= n).Examples: Input : s = "GeeksforGeeks" d = 2Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeek
3 min read
Rotate the matrix right by K times
Given a matrix of size N*M, and a number K. We have to rotate the matrix K times to the right side. Examples: Input : N = 3, M = 3, K = 2 12 23 34 45 56 67 78 89 91 Output : 23 34 12 56 67 45 89 91 78 Input : N = 2, M = 2, K = 2 1 2 3 4 Output : 1 2 3 4 A simple yet effective approach is to consider each row of the matrix as an array and perform an
9 min read
Rotate a matrix by 90 degree in clockwise direction without using any extra space
Given a square matrix, turn it by 90 degrees in a clockwise direction without using any extra space. Examples: Input: 1 2 3 4 5 6 7 8 9 Output: 7 4 1 8 5 2 9 6 3 Input: 1 2 3 4 Output: 3 1 4 2 Method 1 Approach: The approach is similar to Inplace rotate square matrix by 90 degrees | Set 1. The only thing that is different is to print the elements o
15+ min read
Rotate the sub-list of a linked list from position M to N to the right by K places
Given a linked list and two positions 'm' and 'n'. The task is to rotate the sublist from position m to n, to the right by k places.Examples: Input: list = 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6, m = 2, n = 5, k = 2 Output: 1-&gt;4-&gt;5-&gt;2-&gt;3-&gt;6 Rotate the sublist 2 3 4 5 towards right 2 times then the modified list are: 1 4 5 2 3 6 Input: list
13 min read
Python | Ways to rotate a list
The rotation of a list has been discussed earlier also, but this particular article focuses on shorthands and various short techniques to achieve this in one-liners or one word. This operation is quite essential in a programmer's life to achieve various tasks. Let's discuss different ways we can rotate a list in Python. Method 1: Rotate a list usin
8 min read
Practice Tags :
three90RightbarBannerImg