Created
September 5, 2019 12:46
-
-
Save sreeprasad/606a8146263ce26976e8ca7b9aae5f0c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment