Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.35 KB | None | 0 0
  1. public int minValue(int N) {
  2.  
  3. if(N==0) return 1;
  4. if((N&N-1)==0) return 1;
  5. int x = Integer.bitCount(N);
  6.  
  7. int count=0; // is the number of bit set in N
  8. int t = N;
  9. while(t>0){
  10. t>>=1;
  11. count++;
  12. }
  13. int nextHighestPowerOfTwo = 1<<count;
  14. int diff = nextHighestPowerOfTwo-N;
  15. int y = Integer.bitCount(diff)+1;
  16. return Math.min(x,y);
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement