Arrays: Left Rotation using c++














































Arrays: Left Rotation using c++



left rotation operation on an array shifts each of the array's elements  unit to the left. For example, if  left rotations are performed on array , then the array would become .

Given an array  of  integers and a number, , perform  left rotations on the array. Return the updated array to be printed as a single line of space-separated integers.

Input Format

The first line contains two space-separated integers  and , the size of  and the number of left rotations you must perform. 
The second line contains  space-separated integers .

Constraints

Output Format

Print a single line of  space-separated integers denoting the final state of the array after performing  left rotations.


#include<bits/stdc++.h>
using namespace std;

int main(){
int n,k;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
int b[n];
for(int i=0;i<n;i++)
{
int ind=i-k;
if(ind<0)ind+=n;
b[ind]=a[i];
}
for(int i=0;i<n;i++)cout<<b[i]<<" ";
return 0;
}

OUTPUT:


Comments