Circular Array Rotation
Question :-
https://www.hackerrank.com/challenges/circular-array-rotation/problem
Solution in C language :-
#include <stdio.h>
/* function to print an array */
void printArray(int arr[], int size);
/*Fuction to get gcd of a and b*/
int gcd(int a, int b);
/*Function to left rotate arr[] of siz n by d*/
void leftRotate(int arr[], int d, int n)
{
int i, j, k, temp;
int g_c_d = gcd(d, n);
for (i = 0; i < g_c_d; i++) {
/* move i-th values of blocks */
temp = arr[i];
j = i;
while (1) {
k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
}
/*UTILITY FUNCTIONS*/
/* function to print an array */
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
}
/*Fuction to get gcd of a and b*/
int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
/* Driver program to test above functions */
int main()
{
int n,k,q;
scanf("%d%d%d",&n,&k,&q);
int arr[n],i,m[q];
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<q;i++)
scanf("%d",&m[i]);
leftRotate(arr, n-k, n);
/*int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
printArray(arr, 7);
*/
for(i=0;i<q;i++){
printf("%d\n",arr[m[i]]);
}
return 0;
}
Comments
Post a Comment