N = gets.to_i def query(x, y) STDOUT.puts("? #{x} #{y}") STDOUT.flush gets.to_i end (N / 2).times do |i| x = 2 * i y = 2 * i + 1 z = query(x, y) if z == x STDOUT.puts "! #{y}" STDOUT.flush exit elsif z == y STDOUT.puts "! #{x}" STDOUT.flush exit end end STDOUT.puts "! #{N - 1}" STDOUT.flush