-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy patharray_rotation.cpp
More file actions
40 lines (33 loc) · 871 Bytes
/
array_rotation.cpp
File metadata and controls
40 lines (33 loc) · 871 Bytes
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
/*
Problem Statement : Given an array, its lenght and k, right rotate the
array by k units and print the resultant array
*/
#include<bits/stdc++.h>
using namespace std;
// Function to rotate the array
void rotate(int arr[], int n, int k) {
k %= n;
for (int i = 0; i < n; i++) {
if (i < k) {
cout << arr[n + i - k] << " ";
}
else {
cout << (arr[i - k]) << " ";
}
}
cout << "\n";
}
// Main function
int main() {
int n, k;
cout << "Enter the size of the array : " << "\n";
cin >> n;
int arr[n];
cout << "Enter the array elements : " << "\n";
for (int i = 0; i < n; i++) cin >> arr[i];
cout << "Enter the degree of rotation : " << "\n";
cin >> k;
cout << "The resultant array after rotation is : ";
rotate(arr, n, k);
return 0;
}