Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created array_rotation.cpp #260

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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;
}