Skip to main content

Posts

Showing posts with the label Rotating an Array

Rotating an Array

Task: Rotate an array of size N by d elements, where d <= N. Example: Input: 1 5  1 2 3 4 5 Output: 3 4 5 1 2 Here is the Code: Program in C++: #include<iostream> using namespace std; int main()  { int t; cin>>t; while(t--) {     int n;     cin>>n;     int a[n];     for(int i=0;i<n;i++)     {         cin>>a[i];     }     int d;     cin>>d;     // code logic     for(int i=0;i<d;i++)     {         int j=0,temp=a[0];         while(j<n-1)         {             a[j]=a[j+1];             j++;         }         a[j]=temp;     }     for(int i=0;i<n;i++)     {         cout<<a[i]<<" ";     }     cout<<endl; } return 0; } Here is the Video: