Last active
September 23, 2022 20:16
-
-
Save dluciano/822e6e875a95774844490288283cd91c to your computer and use it in GitHub Desktop.
1680. Concatenation of Consecutive Binary Numbers
This file contains 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 class Solution { | |
const int Mod = 1_000_000_007; | |
public int ConcatenatedBinary(int n) { | |
long converted = 0; | |
var msbPosition = 0; | |
for(var i = 1; i <= n; ++i){ | |
if((i & (i - 1)) == 0) | |
msbPosition++; | |
converted = ((converted << msbPosition) | i) % Mod; | |
} | |
return (int)converted; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment