-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodility-cyclic-rotation.c
60 lines (42 loc) · 1.06 KB
/
codility-cyclic-rotation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*CyclicRotation
Rotate an array to the right by a given number of steps.
*
For example, given
A = [3, 8, 9, 7, 6]
K = 3
the function should return [9, 7, 6, 3, 8]. Three rotations were made:
[3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7]
[6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9]
[7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8]*/
#include <stdio.h>
struct Results {
int * A;
int N; // Length of the array
};
void rotate(int A[], int N, int K){
if(K > 0){
int last_element_index = N - 1;
int temp = A[last_element_index];
for(int i=0;i<last_element_index;i++)
A[last_element_index-i] = A[last_element_index-i-1];
A[0] = temp;
rotate(A, N, K-1);
}
}
struct Results solution(int A[], int N, int K){
struct Results result;
result.A = A;
result.N = N;
if(K%N == 0)
return result;
rotate(A, N, K);
return result;
}
int main(){
int A[0];
struct Results result = solution(A, 0, 2);
for(int i=0;i<0;i++){
printf("%d,", A[i]);
}
printf("\n");
}