Skip to content

Commit

Permalink
Added Python solution for "1680. Concatenation of Consecutive Binary …
Browse files Browse the repository at this point in the history
…Numbers"
  • Loading branch information
chemandante committed Oct 4, 2022
1 parent 4a817b7 commit 7e6129d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 16/80/m1680.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Solved:
# (M) Concatenation of Consecutive Binary Numbers
# https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/

class Solution:
def concatenatedBinary(self, n: int) -> int:
res = 1
shift = 1
modulo = 10**9 + 7

for i in range(2, n + 1):
# If `i` is exact power of 2 increase `shift` by 1
if i & (i - 1) == 0:
shift += 1
res = ((res << shift) + i) % modulo

return res


x = Solution()
print(x.concatenatedBinary(1)) # 1
print(x.concatenatedBinary(3)) # 27
print(x.concatenatedBinary(12)) # 505379714
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Here is the list of solved problems. Language images are clickable and lead to t
| 1539 | ![E](img/E.png) [Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/) | | <a href="https://github.com/chemandante/leetcode/blob/master/15/39/m1539.swift" target="_blank"><img src="img/sw.png"></a> |
| 1608 | ![E](img/E.png) [Special Array With X Elements Greater Than or Equal X](https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/) | | <a href="https://github.com/chemandante/leetcode/blob/master/16/08/m1608.swift" target="_blank"><img src="img/sw.png"></a> |
| 1647 | ![M](img/M.png) [Minimum Deletions to Make Character Frequencies Unique](https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/) | | <a href="https://github.com/chemandante/leetcode/blob/master/16/47/m1647.cpp" target="_blank"><img src="img/cpp.png"></a> |
| 1680 | ![M](img/M.png) [Concatenation of Consecutive Binary Numbers](https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/) | | <a href="https://github.com/chemandante/leetcode/blob/master/16/80/m1680.py" target="_blank"><img src="img/py.png"></a> |
| 1689 | ![M](img/M.png) [Partitioning Into Minimum Number Of Deci-Binary Numbers](https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/) | | <a href="https://github.com/chemandante/leetcode/blob/master/16/89/m1689.cpp" target="_blank"><img src="img/cpp.png"></a> |
| 1710 | ![E](img/E.png) [Maximum Units on a Truck](https://leetcode.com/problems/maximum-units-on-a-truck/) | | <a href="https://github.com/chemandante/leetcode/blob/master/17/10/m1710.cpp" target="_blank"><img src="img/cpp.png"></a> |
| 1855 | ![M](img/M.png) [Maximum Distance Between a Pair of Values](https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/) | | <a href="https://github.com/chemandante/leetcode/blob/master/18/55/m1855.py" target="_blank"><img src="img/py.png"></a> |
Expand Down

0 comments on commit 7e6129d

Please sign in to comment.