Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int minValue(int N) {
- if(N==0) return 1;
- if((N&N-1)==0) return 1;
- int x = Integer.bitCount(N);
- int count=0; // is the number of bit set in N
- int t = N;
- while(t>0){
- t>>=1;
- count++;
- }
- int nextHighestPowerOfTwo = 1<<count;
- int diff = nextHighestPowerOfTwo-N;
- int y = Integer.bitCount(diff)+1;
- return Math.min(x,y);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement