I am trying this problem but getting wrong answer I am using binary search in order to search the odd number with k set bits.
spoj.com/SPOJ/problems/BITPLAY/2

import math,sys
def dec_to_bin(x):
    return int(bin(x)[2:])
for i in xrange(input()):
	n,k=map(int,sys.stdin.readline().split())
	if k==1:
		print"1"
		continue
	if k==0:
		print"0"
		continue
	l = 0; u = n + 1;m=0
	ans = 0
	#print n,k
	while(l != u):
		m = l + (u - l) /2
		if(m >= n):
			u = m
			#print m,"out"
			if sum(map(int, str(dec_to_bin(u))))==k and u%2!=0:
				ans = max(ans,u)
		else:
			l = m + 1
			#print l,"inr"
			if sum(map(int, str(dec_to_bin(l))))==k and l%2!=0:
				ans = max(ans,l)
	print ans if ans else -1
  • created

    Aug '15
  • last reply

    Sep '15
  • 1

    reply

  • 791

    views

  • 1

    user

  • 1

    link

10 days later