Search
 
SCRIPT & CODE EXAMPLE
 

CPP

cyclically rotate an array by one

def rotate( arr, n):
    a[0],a[1:]=a[-1],a[:n-1]
    
#Driver Code    
a=[-1,2,3,7,5]
rotate(a,len(a))
print(a)
#Output: [5, -1, 2, 3, 7]
Comment

cyclically rotate an array by one

# Python3 code for program to
# cyclically rotate an array by one
 
# Method for rotation
def rotate(arr, n):
    x = arr[n - 1]
     
    for i in range(n - 1, 0, -1):
        arr[i] = arr[i - 1];
         
    arr[0] = x;
 
 
# 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 ("
Rotated array is")
for i in range(0, n):
    print (arr[i], end = ' ')
 
# This article is contributed
# by saloni1297
Comment

cyclically rotate an array by once

#include <iostream>
using namespace std;
 
void rotate(int arr[], int n)
{
    int i = 0, j = n-1; // i and j pointing to first and last element respectively
      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]);
 
    cout << "Given array is 
";
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
 
    rotate(arr, n);
 
    cout << "
Rotated array is
";
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
 
    return 0;
}
Comment

cyclically rotate an array by one

void rotate(int arr[], int n)
{
    int x=arr[n-1];
    for(int i=n-1;i>0;i--)
    {
        arr[i]=arr[i-1];
    }
    arr[0]=x;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: assignment operator with pointers c++ 
Cpp :: c++ generic pointer 
Cpp :: error uploading arduino code 
Cpp :: shortest path in unweighted graph bfs 
Cpp :: casting to a double in c++ 
Cpp :: How to pass a multidimensional array to a function in C and C++ 
Cpp :: how to find size of int in c++ 
Cpp :: __builtin_popcount 
Cpp :: split string in c++ 
Cpp :: c++ hash 
Cpp :: c++ string concatenation 
Cpp :: c++ else if 
Cpp :: how to create a struct in c++ 
Cpp :: javascript if else exercises 
Cpp :: vector size c++ 
Cpp :: sort 2d vector c++ 
Cpp :: data type c++ 
Cpp :: C++ programming code to remove all characters from string except alphabets 
Cpp :: C++ Initialization of three-dimensional array 
Cpp :: Common elements gfg in c++ 
Cpp :: how to scan vector in c++ 
Cpp :: no of balanced substrings 
Cpp :: c++ camera capture 
Cpp :: convert string to double arduino 
Cpp :: std::is_standard_layout 
Cpp :: c++ argument list for class template is missing 
Cpp :: accepting string with space on same line C++ 
Cpp :: c++ map values range 
Cpp :: tan trigonometric function 
Cpp :: Corong_ExerciseNo3(1) 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =