Coderforces#137 (Div. 2 - A)

Rubyの勉強がてらにCodeforcesの問題を解いてみた。

http://www.codeforces.com/contest/222/problem/A

def solve(k, a)
  t = a[k - 1]
  
  # kより後ろの配列
  other = a[k - 1 .. -1]
  other.each do |i|
    if i != t
      return -1
    end
  end
  
  c = 0
  (k - 1).downto(0) do |idx|
    if a[idx - 1] != t
      return idx
    end
  end
  return c
end

n, k = gets.chomp.split(" ").map{|e| e.to_i}
a = gets.chomp.split(" ").map{|e| e.to_i}
puts solve(k, a)