Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions CPP/array_rotation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}