fork download
  1. #include <iostream>
  2. #include<vector>
  3.  
  4. using namespace std;
  5. // create a function for the given problems
  6. class Solution {
  7. public:
  8. void rotate(vector<int>& nums, int k)
  9. {
  10.  
  11. // If rotation is greater
  12. // than size of array
  13. int n=nums.size();
  14. k = k % n;
  15.  
  16. for(int i = 0; i < n; i++)
  17. {
  18. if(i < k)
  19. {
  20.  
  21. // Printing rightmost
  22. // kth elements
  23. cout << nums[n + i - k] << " ";
  24. }
  25. else
  26. {
  27.  
  28. // Prints array after
  29. // 'k' elements
  30. cout << (nums[i - k]) << " ";
  31. }
  32. }
  33. cout << "\n";
  34. }
  35. };
  36.  
  37.  
  38. int main() {
  39. // your code goes here
  40. Solution s1;
  41. vector<int> v1={3,4,5,6,7};
  42. s1.rotate(v1,3);
  43.  
  44.  
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
5 6 7 3 4