hi, let Avik connect you.


** You can give your thought about this content and request modifications if needed from Ask For Modifications page. Thank You.

** You can check all the posts on the Posts page.

** More about me in the About Me section.

Rotate array


  • Approach:

    1. Insert N numbers into an array say, arr.

    2. Pushed the numbers indexed from i = 0, 1, 2, .... ,K into the arr.

    3. Print elements of arr indexed from i + K, i + 1 + K, i + 2 + K, ... , K+N.


  • Time and Space Complexity:

      • Time Complexity: O(N)

      • Space Complexity: O(N)


Code [C++]

#include <bits/stdc++.h>

#include <iostream>


using namespace std;


int main() {

int n, val;

vector<int>nums;

cin>>n;

for(int i=0; i<n; i++){

cin>>val;

nums.push_back(val);

}

int k;

cin >> k;

for(int i=0; i<k; i++){

nums.push_back(nums[i]);

}

for(int i=0; i<n; i++){

if(i) cout<<' ';

cout<<nums[i+k];

}

return 0;

}