Advertisement
atulsingh7890

Concatenation of Consecutive Binary Numbers

Jan 27th, 2021
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. //https://leetcode.com/explore/challenge/card/january-leetcoding-challenge-2021/582/week-4-january-22nd-january-28th/3618/
  2. class Solution {
  3. public:
  4.     int concatenatedBinary(int n) {
  5.         const int mod = 1e9 + 7;
  6.         long result = 0;
  7.         int length = 0;
  8.         for(int i = 1; i <= n; ++i) {
  9.             if((i & (i-1)) == 0) {
  10.                 length++;
  11.             }
  12.             result = ((result << length) | i) % mod;
  13.         }
  14.         return result;
  15.     }
  16. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement