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.
importjava.io.*;importjava.math.*;importjava.text.*;importjava.util.*;importjava.util.regex.*;importjava.lang.*;publicclassSolution{staticinttable[]=newint[256];staticvoidinitialize(){table[0]=0;for(inti=1;i<256;i++){table[i]=(i&1)+table[i/2];}}staticintcountBit(intn){returntable[n&0xff]+table[(n>>8)&0xff]+table[(n>>16)&0xff]+table[(n>>24)&0xff];}staticinttwosCompliment(inta,intb){intret=0;if(a>=0&&b>=0||a<=0&&b<=0){for(inti=a;i<=b;i++){ret+=countBit(i);}}else{inthashMap[]=newint[b-a+1];for(inti=a;i<=b;i++){// System.out.println(i);if(i==0){ret+=0;}elseif(i<0){inttemp=countBit(i);hashMap[i*-1-1]=temp;ret+=temp;}elseif(i>0&&hashMap[i]!=0){ret+=countBit(i);}elseif(i>0&&hashMap[i]==0){ret+=countBit(i);}}}returnret;}privatestaticfinalScannerscanner=newScanner(System.in);publicstaticvoidmain(String[]args)throwsIOException{intt=Integer.parseInt(scanner.nextLine().trim());initialize();for(inttItr=0;tItr<t;tItr++){// String[] ab = scanner.nextLine().split(" ");// int a = Integer.parseInt(ab[0].trim());// int b = Integer.parseInt(ab[1].trim());inta=scanner.nextInt();intb=scanner.nextInt();// int result =0 ;System.out.println(twosCompliment(a,b));// bufferedWriter.write(String.valueOf(result));// bufferedWriter.newLine();}// bufferedWriter.close();}}
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 →
Can't figure out why it is not working