-
Notifications
You must be signed in to change notification settings - Fork 0
/
Left Rotation.kt
96 lines (68 loc) · 2.21 KB
/
Left Rotation.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
A left rotation operation on an array of size n shifts each of the array's elements 1 unit to the left. Given an integer, d, rotate the array that many steps left and return the result.
Example
d=2
arr=[1,2,3,4,5]
After 2 rotations, arr'=[3,4,5,1,2].
Function Description
Complete the rotateLeft function in the editor below.
rotateLeft has the following parameters:
int d: the amount to rotate by
int arr[n]: the array to rotate
Returns
int[n]: the rotated array
Input Format
The first line contains two space-separated integers that denote n, the number of integers, and d, the number of left rotations to perform.
The second line contains n space-separated integers that describe arr[].
Sample Input
5 4
1 2 3 4 5
Sample Output
5 1 2 3 4
Explanation
To perform d=4 left rotations, the array undergoes the following sequence of changes:
[1,2,3,4,5] -> [2,3,4,5,1] -> [3,4,5,1,2] -> [4,5,1,2,3] -> [5,1,2,3,4]
*/
import java.io.*
import java.math.*
import java.security.*
import java.text.*
import java.util.*
import java.util.concurrent.*
import java.util.function.*
import java.util.regex.*
import java.util.stream.*
import kotlin.collections.*
import kotlin.comparisons.*
import kotlin.io.*
import kotlin.jvm.*
import kotlin.jvm.functions.*
import kotlin.jvm.internal.*
import kotlin.ranges.*
import kotlin.sequences.*
import kotlin.text.*
/*
* Complete the 'rotateLeft' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1. INTEGER d
* 2. INTEGER_ARRAY arr
*/
fun rotateLeft(d: Int, arr: Array<Int>): Array<Int> {
// Write your code here
var rotate = arr.toMutableList()
for (i in 0 until d){
rotate.add(arr[i]) // add the first value of the Array and put in the end of the list
rotate.removeAt(0) // remove the first value of the list
}
return rotate.toTypedArray()
}
fun main(args: Array<String>) {
val first_multiple_input = readLine()!!.trimEnd().split(" ")
val n = first_multiple_input[0].toInt()
val d = first_multiple_input[1].toInt()
val arr = readLine()!!.trimEnd().split(" ").map{ it.toInt() }.toTypedArray()
val result = rotateLeft(d, arr)
println(result.joinToString(" "))
}