Skip to content

Commit 323c919

Browse files
committed
leetcode
1 parent a3f3254 commit 323c919

File tree

4 files changed

+313
-0
lines changed

4 files changed

+313
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
3+
- Concatenation of Consecutive Binary Numbers *-
4+
5+
6+
Given an integer n, return the decimal value of the binary string formed by concatenating the binary representations of 1 to n in order, modulo 109 + 7.
7+
8+
9+
10+
Example 1:
11+
12+
Input: n = 1
13+
Output: 1
14+
Explanation: "1" in binary corresponds to the decimal value 1.
15+
Example 2:
16+
17+
Input: n = 3
18+
Output: 27
19+
Explanation: In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
20+
After concatenating them, we have "11011", which corresponds to the decimal value 27.
21+
Example 3:
22+
23+
Input: n = 12
24+
Output: 505379714
25+
Explanation: The concatenation results in "1101110010111011110001001101010111100".
26+
The decimal value of that is 118505380540.
27+
After modulo 109 + 7, the result is 505379714.
28+
29+
30+
Constraints:
31+
32+
1 <= n <= 105
33+
34+
35+
*/
36+
37+
// Disclaimer : -* Unfortunately this Solution is not available in DART on Leetcode *-
38+
// Solution :- Check the Golang Version for leetcode
39+
40+
import 'dart:math';
41+
42+
class A {
43+
// Bit Manipulation
44+
int concatenatedBinary(int n) {
45+
int mod = (1e9 + 7).toInt();
46+
int res = 0;
47+
for (int i = 1; i <= n; i++) {
48+
int bitLen = (log(i) ~/ log(2)) + 1;
49+
res = ((res << bitLen) + i) % mod;
50+
}
51+
return res;
52+
}
53+
}
54+
55+
class B {
56+
int concatenatedBinary(int n) {
57+
int res = 0;
58+
int bitLength = 1;
59+
int nextIncrease = 2;
60+
61+
for (int i = 1; i <= n; i++) {
62+
if (i == nextIncrease) {
63+
nextIncrease *= 2;
64+
bitLength++;
65+
}
66+
67+
res = (res << bitLength) | i;
68+
res %= 1000000007;
69+
}
70+
71+
return res;
72+
}
73+
}
74+
75+
class C {
76+
int concatenatedBinary(int n) {
77+
int sum = 0;
78+
for (int i = 1; i <= n; i++) {
79+
// String binary = int.toBinaryString(i);
80+
String binary = i.toRadixString(2);
81+
sum = ((sum << binary.length) + i) % 1000000007;
82+
}
83+
return sum;
84+
}
85+
}
86+
87+
// Math
88+
class D {
89+
int concatenatedBinary(int n) {
90+
final int modulo = (1e9 + 7).toInt();
91+
int result = 0;
92+
for (int i = 1; i <= n; i++) {
93+
// For each i, we shift left the position of result with * 2,
94+
// while shifting right the position of i with / 2.
95+
int temp = i;
96+
while (temp > 0) {
97+
temp ~/= 2;
98+
result *= 2;
99+
}
100+
// Add the i to the result and get the remainder of modulo.
101+
result = (result + i) % modulo;
102+
}
103+
return result;
104+
}
105+
}
106+
107+
class E {
108+
int concatenatedBinary(int n) {
109+
int digits = 0, MOD = 1000000007;
110+
int result = 0;
111+
for (int i = 1; i <= n; i++) {
112+
/* if "i" is a power of 2, then we have one additional digit in
113+
its the binary equivalent compared to that of i-1 */
114+
if ((i & (i - 1)) == 0) digits++;
115+
result = ((result << digits) + i) % MOD;
116+
}
117+
118+
return result;
119+
}
120+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package main
2+
3+
// Time Complexity: O(N)
4+
// Space Complexity: O(1)
5+
6+
// the idea is to use bit manipulation to set the current number based on the previous number
7+
// for example,
8+
// n = 1, ans = 0b1
9+
// n = 2 (10), we need to shift 2 bits of the previous ans to the left and add `n`
10+
// i.e. 1 -> 100 (shift 2 bits to the left) -> 110 (set `10`). ans = 0b110
11+
// n = 3 (11), we need to shift 2 bits of the previous ans to the left and add `n`
12+
// i.e 110 -> 11000 (shift 2 bits to the left) -> 11011 (set `11`). ans = 0b11011
13+
// n = 4 (100), we need to shift 3 bits of the previous ans to the left and add `n`
14+
// i.e. 11011 -> 11011000 (shift 3 bits to the left) -> 11011100 (set `100). ans = 0b11011100
15+
// so now we can see a pattern here
16+
// we need to shift `l` bits of the previous ans to the left and add the current `i`
17+
// how to know `l`? it is not difficult to see `x` only increases when we meet power of 2
18+
19+
func concatenatedBinary(n int) int {
20+
// `l` is the bit length to be shifted
21+
ans, l, M := 0, 0, 1_000_000_007
22+
for i := 1; i <= n; i++ {
23+
// i & (i - 1) means removing the rightmost set bit
24+
// e.g. 100100 -> 100000
25+
// 000001 -> 000000
26+
// 000000 -> 000000
27+
// after removal, if it is 0, then it means it is power of 2
28+
// as all power of 2 only contains 1 set bit
29+
// if it is power of 2, we increase the bit length `l`
30+
if i&(i-1) == 0 {
31+
l += 1
32+
}
33+
// (ans << l) means shifting the original answer `l` bits to th left
34+
// (x | i) means using OR operation to set the bit
35+
// e.g. 0001 << 3 = 0001000
36+
// e.g. 0001000 | 0001111 = 0001111
37+
ans = ((ans << l) | i) % M
38+
}
39+
return ans
40+
}
41+
42+
// ------------------------------------------------------------
43+
44+
// func concatenatedBinary(n int) int {
45+
// mod := int(math.Pow(10, 9)) + 7
46+
// num := 0
47+
// length := 0
48+
// for i := 1; i <= n; i++ {
49+
// if int(math.Pow(2, float64(int(math.Log(float64(i)) / math.Log(2))))) == i {
50+
// length++
51+
// }
52+
// num <<= length
53+
// num += i
54+
// num %= mod
55+
// }
56+
// return num
57+
// }
58+
59+
// ------------------------------------------------------------
60+
61+
// func concatenatedBinary(n int) int {
62+
// ans := 1
63+
64+
// current, count := 2, 1
65+
// for i := 2; i <= n; i++ {
66+
// if i == current {
67+
// current <<= 1
68+
// count++
69+
// }
70+
71+
// // so i is count bit
72+
// ans <<= count
73+
// ans += i
74+
// ans %= 1_000_000_007
75+
// }
76+
77+
// return ans
78+
// }
79+
// ------------------------------------------------------------
80+
// func concatenatedBinary(n int) int {
81+
// length, res, mod := 0, 0, 1000000007
82+
83+
// for i:=1; i<=n; i++ {
84+
// if i & (i-1) == 0 {
85+
// length++
86+
// }
87+
// res = (res << length | i) % mod
88+
// }
89+
// return res
90+
// }
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# 🔥 Dart 🔥 || 5 solution || line by line explanation
2+
3+
## Warning:-
4+
5+
At the time of writing this solution You can't submit on leetcode because there is not option for `Dart` But i did my best to implement it in `DART` because in future if leetcode support dart for this specific solution than things will workout for us `HOPE SO`
6+
7+
### Solution - 1 Bit Manipulation
8+
9+
```dart
10+
class Solution {
11+
int concatenatedBinary(int n) {
12+
int mod = (1e9 + 7).toInt();
13+
int res = 0;
14+
for (int i = 1; i <= n; i++) {
15+
int bitLen = (log(i) ~/ log(2)) + 1;
16+
res = ((res << bitLen) + i) % mod;
17+
}
18+
return res;
19+
}
20+
}
21+
```
22+
23+
### Solution 3 -
24+
25+
```dart
26+
class Solution {
27+
int concatenatedBinary(int n) {
28+
int res = 0;
29+
int bitLength = 1;
30+
int nextIncrease = 2;
31+
32+
for (int i = 1; i <= n; i++) {
33+
if (i == nextIncrease) {
34+
nextIncrease *= 2;
35+
bitLength++;
36+
}
37+
38+
res = (res << bitLength) | i;
39+
res %= 1000000007;
40+
}
41+
42+
return res;
43+
}
44+
}
45+
```
46+
47+
### Solution - 3
48+
49+
```dart
50+
class Solution {
51+
int concatenatedBinary(int n) {
52+
int sum = 0;
53+
for (int i = 1; i <= n; i++) {
54+
String binary = i.toRadixString(2);
55+
sum = ((sum << binary.length) + i) % 1000000007;
56+
}
57+
return sum;
58+
}
59+
}
60+
```
61+
62+
### Solution - 4 Math
63+
64+
```dart
65+
class Solution {
66+
int concatenatedBinary(int n) {
67+
final int modulo = (1e9 + 7).toInt();
68+
int result = 0;
69+
for (int i = 1; i <= n; i++) {
70+
// For each i, we shift left the position of result with * 2,
71+
// while shifting right the position of i with / 2.
72+
int temp = i;
73+
while (temp > 0) {
74+
temp ~/= 2;
75+
result *= 2;
76+
}
77+
// Add the i to the result and get the remainder of modulo.
78+
result = (result + i) % modulo;
79+
}
80+
return result;
81+
}
82+
}
83+
```
84+
85+
### Solution - 5
86+
87+
```dart
88+
class Solution {
89+
int concatenatedBinary(int n) {
90+
int digits = 0, MOD = 1000000007;
91+
int result = 0;
92+
for (int i = 1; i <= n; i++) {
93+
/* if "i" is a power of 2, then we have one additional digit in
94+
its the binary equivalent compared to that of i-1 */
95+
if ((i & (i - 1)) == 0) digits++;
96+
result = ((result << digits) + i) % MOD;
97+
}
98+
99+
return result;
100+
}
101+
}
102+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
4242
* [Two Sum](TwoSum/twosum.dart)
4343
* [UFT-8 Validation](UTF-8Validation/uft_8_validation.dart)
4444
* [Valid Parenthesis](ValidParentheses/valid_parentheses.dart)
45+
* [Concatenation of Consecutive Binary Numbers](ConcatenationofConsecutiveBinaryNumbers/concatenation_of_consecutive_binary_numbers.dart)
4546

4647
## Reach me via
4748

0 commit comments

Comments
 (0)