-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircular_array_rotation.rb
More file actions
52 lines (37 loc) · 920 Bytes
/
circular_array_rotation.rb
File metadata and controls
52 lines (37 loc) · 920 Bytes
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
#!/bin/ruby
require 'json'
require 'stringio'
#
# Complete the 'circularArrayRotation' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
# 1. INTEGER_ARRAY a
# 2. INTEGER k
# 3. INTEGER_ARRAY queries
#
def rotation_anti_clock(a, k)
k.times do
a.unshift(a.pop)
end
a
end
def circularArrayRotation(a, k, queries)
b = rotation_anti_clock(a,k)
queries.map { |i| b[i]}
end
fptr = File.open(ENV['OUTPUT_PATH'], 'w')
first_multiple_input = gets.rstrip.split
n = first_multiple_input[0].to_i
k = first_multiple_input[1].to_i
q = first_multiple_input[2].to_i
a = gets.rstrip.split.map(&:to_i)
queries = Array.new(q)
q.times do |i|
queries_item = gets.strip.to_i
queries[i] = queries_item
end
result = circularArrayRotation a, k, queries
fptr.write result.join "\n"
fptr.write "\n"
fptr.close()