Skip to content

Commit 5f38de2

Browse files
Update and rename Rotate Array (LeetCode).cpp to Rotate Array (LeetCode)-Top Interview Que-Easy Level.cpp
1 parent f46cec2 commit 5f38de2

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution //Solution Class
2+
{
3+
public:
4+
void rotate(vector<int>& nums, int k) //function for shifting the elements
5+
{
6+
int n = nums.size(); //assign size of array
7+
int start = n - k % n; //starting from first element
8+
int remainder = k % n; //to move to next index
9+
vector<int> v(n);
10+
11+
for(int i = 0; i < remainder; i++) //shifting the index
12+
{
13+
v[i] = nums[i + start];
14+
}
15+
16+
for(int i = 0; i < start; i++) //shifting the index
17+
{
18+
v[i + remainder] = nums[i];
19+
}
20+
21+
for(int i = 0; i < n; i++) //for shifting elements
22+
{
23+
nums[i] = v[i];
24+
}
25+
}
26+
};
27+

Arrays/Rotate Array (LeetCode).cpp

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)