-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotatearray.cpp
69 lines (56 loc) · 1.19 KB
/
Rotatearray.cpp
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
61
62
63
64
65
66
67
68
69
// You have been given a random integer array/list(ARR) of size N. Write a function that rotates the given array/list by D elements(towards the left).
// Note: Change in the input array/list itself. You don't need to return or print the elements.
// Sample Input 1:
// 1
// 7
// 1 2 3 4 5 6 7
// 2
// Sample Output 1:
// 3 4 5 6 7 1 2
// My Code:
void rvereseArray(int arr[], int start, int end)
{
while (start < end)
{
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
void rotate(int *input, int d, int n)
{
rvereseArray(input,0,n-1); // reversing the complete array
rvereseArray(input,0,n-d-1); // reversing first n-d elements
rvereseArray(input,n-d,n-1); // reversing last d elements
}
// Main Code:
#include <iostream>
using namespace std;
#include "solution.h"
int main()
{
int t;
cin >> t;
while (t--)
{
int size;
cin >> size;
int *input = new int[size];
for (int i = 0; i < size; ++i)
{
cin >> input[i];
}
int d;
cin >> d;
rotate(input, d, size);
for (int i = 0; i < size; ++i)
{
cout << input[i] << " ";
}
delete[] input;
cout << endl;
}
return 0;
}