Skip to content

Commit 0ebc31f

Browse files
authored
Merge pull request ashutosh97#227 from rohitjakhar/master
Add Rotate the Array Question
2 parents e418651 + e6cf1b6 commit 0ebc31f

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

Array/Rotate Array/Question.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
We have to rotate the elements of the given array k times to the right.

Array/Rotate Array/solution.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
internal class Solution {
2+
fun rotate(nums:IntArray, k:Int) {
3+
k = k % nums.size
4+
val count = 0
5+
val start = 0
6+
while (count < nums.size)
7+
{
8+
val current = start
9+
val prev = nums[start]
10+
do
11+
{
12+
val next = (current + k) % nums.size
13+
val temp = nums[next]
14+
nums[next] = prev
15+
prev = temp
16+
current = next
17+
count++
18+
}
19+
while (start != current)
20+
start++
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)