Skip to content

Commit 46b41b5

Browse files
committed
added problem 1680
1 parent 91114eb commit 46b41b5

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

problems/problem1680/binary_concat.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package problem1680
2+
3+
/*
4+
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.
5+
*/
6+
7+
const MOD = 1000000007
8+
9+
func concatenatedBinary(n int) int {
10+
var res, max, size int
11+
// size is the current length of the binary number
12+
// max is the maximal number of size binary length
13+
max, size = 1, 1
14+
for i := 1; i <= n; i++ {
15+
// need to displace the result by the size of the next number
16+
res <<= size
17+
// and place the current number at the start
18+
res |= i
19+
// if next number will be longer
20+
if i == max {
21+
// adjust max and size
22+
max <<= 1
23+
max++
24+
size++
25+
}
26+
res %= MOD
27+
}
28+
return res
29+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package problem1680
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type Result struct {
11+
Input int
12+
Expected int
13+
}
14+
15+
var Results = []Result{
16+
{1, 1},
17+
{3, 27},
18+
{12, 505379714},
19+
{10000, 356435599},
20+
{100000, 757631812},
21+
}
22+
23+
func TestConcatBinaryConsecutive(t *testing.T) {
24+
assert := assert.New(t)
25+
26+
for _, res := range Results {
27+
want := res.Expected
28+
got := concatenatedBinary(res.Input)
29+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
30+
}
31+
}

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ Each problem is in it's own directory, with test files. There are helper package
169169
| 1642 | [Furthest Building You Can Reach](https://leetcode.com/problems/furthest-building-you-can-reach) | [My Solution](./problems/problem1642) ||
170170
| 1647 | [Minimum Deletions to Make Character Frequencies Unique](https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique) | [My Solution](./problems/problem1647) ||
171171
| 1658 | [Minimum Operations to Reduce X to Zero](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero) | [My Solution](./problems/problem1658) ||
172+
| 1680 | [Concatenation of Consecutive Binary Numbers](https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers) | [My Solution](./problems/problem1680) ||
172173
| 1689 | [Partitioning Into Minimum Number Of Deci-Binary Numbers](https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers) | [My Solution](./problems/problem1689) ||
173174
| 1695 | [Maximum Erasure Value](https://leetcode.com/problems/maximum-erasure-value) | [My Solution](./problems/problem1695) ||
174175
| 1696 | [Jump Game VI](https://leetcode.com/problems/jump-game-vi) | [My Solution](./problems/problem1696) ||

0 commit comments

Comments
 (0)