We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
# Enter your code here. Read input from STDIN. Print output to STDOUTdefcount1s(n,i):ifi==1:returnn&1mask=1<<i-1remainder=(0xFFFFFFFF>>(32-i+1))&nreturn(n&mask>0)*((mask*(i-1)>>1)+1+remainder)+count1s(remainder,i-1)defstartCount():iterations=int(input())lis=[]whileiterations>0:n1,n2=[int(x)forxininput().split()]lis.append([n1,n2])iterations-=1forelementinlis:if(element[0]>0)or(element[1]<0):print(count1s(element[1],32)-count1s(element[0]-1,32))else:print((1<<36)-count1s(element[0]-1,32)+count1s(element[1],32))startCount()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
2's complement
You are viewing a single comment's thread. Return to all comments →
Python3 solution