I am using hacker rank and I do not understand why my ruby code only works for one test case out of like 20. Here is the question:
John Watson knows of an operation called a right circular rotation on an array of integers. One rotation operation moves the last array element to the first position and shifts all remaining elements right one. To test Sherlock's abilities, Watson provides Sherlock with an array of integers. Sherlock is to perform the rotation operation a number of times then determine the value of the element at a given position.
For each array, perform a number of right circular rotations and return the values of the elements at the given indices.
Function Description
Complete the circularArrayRotation function in the editor below.
circularArrayRotation has the following parameter(s):
int a[n]: the array to rotateint k: the rotation countint queries[1]: the indices to reportReturns
int[q]: the values in the rotatedaas requested inmInput Format
The first line contains 3 space-separated integers,
n,k, andq, the number of elements in the integer array, the rotation count and the number of queries. The second line containsnspace-separated integers, where each integeridescribes array elementa[i](where0 <= i < n). Each of theqsubsequent lines contains a single integer,queries[i], an index of an element inato return.Constraints
Sample Input 0
3 2 3 1 2 3 0 1 2Sample Output 0
2 3 1
Here is my code :
def circularArrayRotation(a, k, queries)
q = []
while k >= 1
m = a.pop()
a.unshift m
k = k - 1
end
for i in queries do
v = a[queries[i]]
q.push v
end
return q
end
It only works for the sample text case but I can't figure out why. Thanks for any help you can provide.
CodePudding user response:
A little help with the parsing of the input:
#!/usr/bin/env ruby
input = ARGF.readlines.map(&:split)
n,k,q = input.shift.map(&:to_i)
a = input.shift.map(&:to_i)
queries = input.flatten.map(&:to_i)
puts <<-EOF
n = #{n}
k = #{k}
q = #{q}
a = #{a}
queries = #{queries}
EOF
output (from given input):
n = 3
k = 2
q = 3
a = [1, 2, 3]
queries = [0, 1, 2]
CodePudding user response:
I don't see any errors in your code, but I would like to suggest a more efficient way of making the calculation.
First observe that after q rotations the element at index i will at index (i q) % n.
For example, suppose
n = 3
a = [1,2,3]
q = 5
Then after q rotations the array will be as follows.
arr = Array.new(3)
arr[(0 5) % 3] = a[0] #=> arr[2] = 1
arr[(1 5) % 3] = a[1] #=> arr[0] = 2
arr[(2 5) % 3] = a[2] #=> arr[1] = 3
arr #=> [2,3,1]
We therefore can write
def doit(n,a,q,queries)
n.times.with_object(Array.new(n)) do |i,arr|
arr[(i q) % n] = a[i]
end.values_at(*queries)
end
doit(3,[1,2,3],5,[0,1,2])
#=> [2,3,1]
doit(3,[1,2,3],5,[2,1])
#=> [1, 3]
doit(3,[1,2,3],2,[0,1,2])
#=> [2, 3, 1]
p doit(3,[1,2,3],0,[0,1,2])
#=> [1,2,3]
doit(20,(0..19).to_a,25,(0..19).to_a.reverse)
#=> [14, 13, 12, 11, 10, 9, 8, 7, 6, 5,
# 4, 3, 2, 1, 0, 19, 18, 17, 16, 15]
Alternatively, we may observe that after q rotations the element at index j was initially at index (j n-q) % n.
For the earlier example, letting nmq = n-q = 3-5 = -2, after q rotations the array will be
[a[(0-2) % 3], a[(1-2) % 3], a[(2-2) % 3]]
#=> [a[1], a[2], a[0]]
#=> [2,3,1]
We therefore could instead write
def doit(n,a,q,queries)
nmq = n-q
n.times.map { |j| a[(j nmq) % n] }.values_at(*queries)
end
