|
| 1 | +--- |
| 2 | +id: non-negative-integers-without-consecutive-ones |
| 3 | +title: Non-negative Integers without Consecutive Ones |
| 4 | +sidebar_label: Non-negative Integers without Consecutive Ones |
| 5 | +tags: |
| 6 | + - Dynamic Programming |
| 7 | + - Bit Manipulation |
| 8 | + - Algorithms |
| 9 | + - DSA |
| 10 | + - Python |
| 11 | + - C++ |
| 12 | + - Java |
| 13 | + - JavaScript |
| 14 | +description: "This page explains the problem of finding non-negative integers without consecutive ones." |
| 15 | +--- |
| 16 | + |
| 17 | +# Non-negative Integers without Consecutive Ones |
| 18 | + |
| 19 | +## 1. Problem Description |
| 20 | + |
| 21 | +Given a non-negative integer $n$, find the number of non-negative integers less than or equal to $n$ that do not contain consecutive ones in their binary representation. |
| 22 | + |
| 23 | +## 2. Examples |
| 24 | + |
| 25 | +### Example 1: |
| 26 | +**Input:** $n = 5$ |
| 27 | +**Output:** $5$ |
| 28 | +**Explanation:** |
| 29 | +There are five such numbers: 0, 1, 2, 4, 5. |
| 30 | +Their binary representations are: 0 (0), 1 (1), 2 (10), 4 (100), 5 (101). |
| 31 | + |
| 32 | +### Example 2: |
| 33 | +**Input:** $n = 10$ |
| 34 | +**Output:** $8$ |
| 35 | +**Explanation:** |
| 36 | +There are eight such numbers: 0, 1, 2, 4, 5, 8, 9, 10. |
| 37 | +Their binary representations are: 0 (0), 1 (1), 2 (10), 4 (100), 5 (101), 8 (1000), 9 (1001), 10 (1010). |
| 38 | + |
| 39 | +## 3. Constraints |
| 40 | + |
| 41 | +- $0 <= n <= 10^9$ |
| 42 | + |
| 43 | +## 4. Algorithm |
| 44 | + |
| 45 | +### Approach: |
| 46 | +The problem can be approached using dynamic programming. We need to count the numbers up to $n$ that do not have consecutive ones in their binary representation. This can be solved by considering the binary digits of $n$. |
| 47 | + |
| 48 | +1. **Initialize Variables:** |
| 49 | + - $dp0[i]$: Number of valid integers of length $i$ ending with $0$. |
| 50 | + - $dp1[i]$: Number of valid integers of length $i$ ending with $1$. |
| 51 | + |
| 52 | +2. **Base Case:** |
| 53 | + - $dp0[1] = 1$: Only one number of length 1 ending with $0$ (i.e., $0$). |
| 54 | + - $dp1[1] = 1$: Only one number of length 1 ending with $1$ (i.e., $1$). |
| 55 | + |
| 56 | +3. **DP Transition:** |
| 57 | + - For each bit position from 2 to the length of $n$ in binary: |
| 58 | + - $dp0[i] = dp0[i-1] + dp1[i-1]$ : If the current bit is $0$, the previous bit can be either $0$ or $1$. |
| 59 | + - $dp1[i] = dp0[i-1]$ : If the current bit is $1$, the previous bit must be $0$. |
| 60 | + |
| 61 | +4. **Calculate Result:** |
| 62 | + - Sum up all the valid numbers up to the highest bit of $n$. |
| 63 | + |
| 64 | +### Example Calculation: |
| 65 | + |
| 66 | +For $n = 5$ (binary $101$): |
| 67 | +1. Calculate the number of valid integers for each bit length. |
| 68 | +2. Use the DP arrays to count valid integers. |
| 69 | + |
| 70 | +## 5. Implementation (Code for 4 Languages) |
| 71 | + |
| 72 | +<Tabs> |
| 73 | + <TabItem value="Python" label="Python" default> |
| 74 | + ```python |
| 75 | + def findIntegers(n): |
| 76 | + bin_n = bin(n)[2:] |
| 77 | + length = len(bin_n) |
| 78 | + dp0 = [0] * (length + 1) |
| 79 | + dp1 = [0] * (length + 1) |
| 80 | + dp0[1] = dp1[1] = 1 |
| 81 | + for i in range(2, length + 1): |
| 82 | + dp0[i] = dp0[i - 1] + dp1[i - 1] |
| 83 | + dp1[i] = dp0[i - 1] |
| 84 | + result = dp0[length] + dp1[length] |
| 85 | + for i in range(1, length): |
| 86 | + if bin_n[i] == '1' and bin_n[i - 1] == '1': |
| 87 | + break |
| 88 | + if bin_n[i] == '0' and bin_n[i - 1] == '0': |
| 89 | + result -= dp1[length - i] |
| 90 | + return result |
| 91 | + |
| 92 | + # Example usage: |
| 93 | + print(findIntegers(5)) # Output: 5 |
| 94 | + ``` |
| 95 | + </TabItem> |
| 96 | + |
| 97 | + <TabItem value="C++" label="C++"> |
| 98 | + ```cpp |
| 99 | + #include <iostream> |
| 100 | + #include <vector> |
| 101 | + using namespace std; |
| 102 | + |
| 103 | + int findIntegers(int n) { |
| 104 | + vector<int> bin; |
| 105 | + while (n) { |
| 106 | + bin.push_back(n % 2); |
| 107 | + n /= 2; |
| 108 | + } |
| 109 | + int length = bin.size(); |
| 110 | + vector<int> dp0(length + 1, 0), dp1(length + 1, 0); |
| 111 | + dp0[1] = dp1[1] = 1; |
| 112 | + for (int i = 2; i <= length; ++i) { |
| 113 | + dp0[i] = dp0[i - 1] + dp1[i - 1]; |
| 114 | + dp1[i] = dp0[i - 1]; |
| 115 | + } |
| 116 | + int result = dp0[length] + dp1[length]; |
| 117 | + for (int i = length - 2; i >= 0; --i) { |
| 118 | + if (bin[i] == 1 && bin[i + 1] == 1) break; |
| 119 | + if (bin[i] == 0 && bin[i + 1] == 0) result -= dp1[i + 1]; |
| 120 | + } |
| 121 | + return result; |
| 122 | + } |
| 123 | + |
| 124 | + // Example usage: |
| 125 | + int main() { |
| 126 | + cout << findIntegers(5) << endl; // Output: 5 |
| 127 | + return 0; |
| 128 | + } |
| 129 | + ``` |
| 130 | + </TabItem> |
| 131 | +
|
| 132 | + <TabItem value="Java" label="Java"> |
| 133 | + ```java |
| 134 | + public class NonNegativeIntegers { |
| 135 | + public int findIntegers(int n) { |
| 136 | + String binN = Integer.toBinaryString(n); |
| 137 | + int length = binN.length(); |
| 138 | + int[] dp0 = new int[length + 1]; |
| 139 | + int[] dp1 = new int[length + 1]; |
| 140 | + dp0[1] = dp1[1] = 1; |
| 141 | + for (int i = 2; i <= length; i++) { |
| 142 | + dp0[i] = dp0[i - 1] + dp1[i - 1]; |
| 143 | + dp1[i] = dp0[i - 1]; |
| 144 | + } |
| 145 | + int result = dp0[length] + dp1[length]; |
| 146 | + for (int i = 1; i < length; i++) { |
| 147 | + if (binN.charAt(i) == '1' && binN.charAt(i - 1) == '1') break; |
| 148 | + if (binN.charAt(i) == '0' && binN.charAt(i - 1) == '0') result -= dp1[length - i]; |
| 149 | + } |
| 150 | + return result; |
| 151 | + } |
| 152 | +
|
| 153 | + public static void main(String[] args) { |
| 154 | + NonNegativeIntegers solution = new NonNegativeIntegers(); |
| 155 | + System.out.println(solution.findIntegers(5)); // Output: 5 |
| 156 | + } |
| 157 | + } |
| 158 | + ``` |
| 159 | + </TabItem> |
| 160 | + |
| 161 | + <TabItem value="JavaScript" label="JavaScript"> |
| 162 | + ```javascript |
| 163 | + function findIntegers(n) { |
| 164 | + const binN = n.toString(2); |
| 165 | + const length = binN.length; |
| 166 | + const dp0 = new Array(length + 1).fill(0); |
| 167 | + const dp1 = new Array(length + 1).fill(0); |
| 168 | + dp0[1] = dp1[1] = 1; |
| 169 | + for (let i = 2; i <= length; i++) { |
| 170 | + dp0[i] = dp0[i - 1] + dp1[i - 1]; |
| 171 | + dp1[i] = dp0[i - 1]; |
| 172 | + } |
| 173 | + let result = dp0[length] + dp1[length]; |
| 174 | + for (let i = 1; i < length; i++) { |
| 175 | + if (binN[i] === '1' && binN[i - 1] === '1') break; |
| 176 | + if (binN[i] === '0' && binN[i - 1] === '0') result -= dp1[length - i]; |
| 177 | + } |
| 178 | + return result; |
| 179 | + } |
| 180 | + |
| 181 | + // Example usage: |
| 182 | + console.log(findIntegers(5)); // Output: 5 |
| 183 | + ``` |
| 184 | + </TabItem> |
| 185 | +</Tabs> |
| 186 | + |
| 187 | +## 8. Complexity Analysis |
| 188 | + |
| 189 | +- **Time Complexity:** $O(log n)$, where $n$ is the input number. This is because we are working with the binary representation of $n$, which has a length of $O(log n)$. |
| 190 | +- **Space Complexity:** $O(log n)$, due to the storage of the $dp0$ and $dp1$ arrays and the binary representation of $n$. |
| 191 | + |
| 192 | +## 9. Advantages and Disadvantages |
| 193 | + |
| 194 | +### Advantages: |
| 195 | +- Efficiently solves the problem using dynamic programming. |
| 196 | +- Works well for large values of $n$ up to $(10^9)$. |
| 197 | + |
| 198 | +### Disadvantages: |
| 199 | +- Requires understanding of bit manipulation and dynamic programming concepts. |
| 200 | + |
| 201 | +## 10. References |
| 202 | + |
| 203 | +- [GeeksforGeeks - Non-negative Integers without Consecutive Ones](https://www.geeksforgeeks.org/non-negative-integers-without-consecutive-ones/) |
| 204 | +- [LeetCode - Non-negative Integers without Consecutive Ones](https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/) |
| 205 | + |
| 206 | + |
| 207 | +- **Author's Geeks for Geeks Profile:** [MuraliDharan](https://www.geeksforgeeks.org/user/ngmuraqrdd/) |
| 208 | + |
| 209 | +This Markdown file includes a detailed explanation of the problem, an algorithmic approach, code implementations in four programming languages, complexity analysis, advantages and disadvantages, and references. |
0 commit comments