hackerrank/interview_preparation_kit_arrays.rb
# Complete the minimumBribes function below.

Complete the minimumBribes function below.
Complete the minimumBribes function below .
def minimumBribes(q)
  moves = 0
  r = (0..q.size-1).to_a
  until q == (1..q.size).to_a do
    q.map { |a| a - 1 }
      .reverse_each.with_index do |person, i|
      i = q.size - i - 1
      if person - i > 2
        puts "Too chaotic"
        return
      end
      if person > r[i] && person > q[person] -1
        moves += person - r[i]
        q = q - [person + 1]
        q.insert(person, person + 1)
      end
    end
  end
  puts moves
end
def minimumBribes ( q ) moves = 0 r = ( 0 .. q . size - 1 ) . to_a until q == ( 1 .. q . size ) . to_a do q . map { | a | a - 1 } . reverse_each . with_index do | person , i | i = q . size - i - 1 if person - i > 2 puts "Too chaotic" return end if person > r [ i ] && person > q [ person ] - 1 moves += person - r [ i ] q = q - [ person + 1 ] q . insert ( person , person + 1 ) end end end puts moves end
Complete the minimumBribes function below.
Complete the minimumBribes function below .
q
<not_specific>