Given integer value n in array to rotate the element in c++ program

 Approach : 

It is used as a simple approach to rotate the given array.


#include <iostream>

using namespace std;

void rotate(int *arr, int n, int k){

    int tmp, t;

    for(int i=k; i<=n; i++){

       tmp=arr[i];

       cout<<arr[i]<<" ";

    }

    for(int i=0; i<k; i++){

        cout<<arr[i]<<" ";

    }

}


int main()

{

    int n=5;

    int arr[n]={1,2,3,4,5,6};

    int k=3;

    rotate(arr,n, k);

    return 0;

}


Output is :

4 5 6 1 2 3 

Post a Comment

0 Comments