SHARE
TWEET

Untitled

a guest Sep 16th, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top