HackerRank Left Rotation solution in C/C++
Left Rotation in Array
A left rotation operation on an array of size shifts each of the array's elements unit to the left. Given an integer, , rotate the array that many steps left and return the result.
Example
After rotations, .
Function Description
Complete the rotateLeft function in the editor below.
rotateLeft has the following parameters:
- int d: the amount to rotate by
- int arr[n]: the array to rotate
Returns
- int[n]: the rotated array
Input Format
The first line contains two space-separated integers that denote , the number of integers, and , the number of left rotations to perform.
The second line contains space-separated integers that describe .
Constraints
Sample Input
5 4
1 2 3 4 5
Sample Output
5 1 2 3 4
Explanation
To perform left rotations, the array undergoes the following sequence of changes:
Solution: C/C++ code
#include<stdio.h>
int main()
{
int i=0;
int siz,z=0;
int rot;
scanf("%d %d",&siz,&rot);
//if(siz>100000 || siz<=0)
//exit(0);
//if(rot>siz || rot<=0)
//exit(0);
int arr[siz];
for(i=0;i<siz;i++)
{
scanf("%d",&arr[i]);
//if(arr[i]>1000000 || arr[i]<=0)
//exit(0);
}
for (int i = 0; i < siz; i++)
{
printf("%d ", arr[(i + rot) % siz]);
}
}
Comments
Post a Comment