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

Complete the minimumSwaps function below.
Complete the minimumSwaps function below .
def minimumSwaps(arr)
  swap_cnt = 0
  valid_arr = (1..arr.size).to_a
  until arr == valid_arr do
    arr.each_with_index do |elem, i|
      if elem > valid_arr[i]
        target = arr.index(i+1)
        arr[i] = valid_arr[i]
        arr[target] = elem
        swap_cnt += 1
      end
    end
  end
  swap_cnt
end
def minimumSwaps ( arr ) swap_cnt = 0 valid_arr = ( 1 .. arr . size ) . to_a until arr == valid_arr do arr . each_with_index do | elem , i | if elem > valid_arr [ i ] target = arr . index ( i + 1 ) arr [ i ] = valid_arr [ i ] arr [ target ] = elem swap_cnt += 1 end end end swap_cnt end
Complete the minimumSwaps function below.
Complete the minimumSwaps function below .
arr
<not_specific>