commit 13824fc1f6b424fe1a12a8e407db8fc467742ee3
parent 1aaf4fd380c221dfa2ed7e03d23d663214678893
Author: Jack Sangdahl <jack@pngu.org>
Date: Fri, 27 Feb 2026 21:28:36 -0700
1680. concatenation of consecutive binary numbers
Diffstat:
2 files changed, 52 insertions(+), 0 deletions(-)
diff --git a/concat_consec_binary_num.c b/concat_consec_binary_num.c
@@ -0,0 +1,30 @@
+// 1680. Concatenation of Consecutive Binary Numbers
+#include "leetcode.h"
+
+/*
+ * given an integer 'n', return the decimal value of the binary string formed by
+ * concatenating the binary rerpesentations of 1 to 'n' in order, modulo 10^9
+ * + 7.
+ */
+
+int concatenatedBinary(int n) {
+ long ans = 0, mod = 1e9 + 7, len = 0;
+ for (int i = 1; i <= n; ++i) {
+ if (__builtin_popcount(i) == 1)
+ ++len;
+ ans = ((ans << len) % mod + i) % mod;
+ }
+ return ans;
+}
+
+int main() {
+ int r1 = concatenatedBinary(1);
+ int r2 = concatenatedBinary(3);
+ int r3 = concatenatedBinary(12);
+ printf("%d\n", r1); // expect: 1
+ assert(r1 == 1);
+ printf("%d\n", r2); // expect: 27
+ assert(r2 == 27);
+ printf("%d\n", r3); // expect: 505379714
+ assert(r3 == 505379714);
+}
diff --git a/concat_consec_binary_num.py b/concat_consec_binary_num.py
@@ -0,0 +1,22 @@
+# 1680. Concatenation of Consecutive Binary Numbers
+
+"""
+given an integer 'n', return the decimal value of the binary string formed by
+concatenating the binary rerpesentations of 1 to 'n' in order, modulo 10^9
++ 7.
+"""
+
+
+class Solution:
+ def concatenatedBinary(self, n: int) -> int:
+ ans = ""
+ for i in range(1, n + 1):
+ ans += bin(i)[2:]
+ return int(ans, 2) % (10**9 + 7)
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.concatenatedBinary(n=1))
+ print(obj.concatenatedBinary(n=3))
+ print(obj.concatenatedBinary(n=12))