|
| 1 | +--- |
| 2 | +id: special-numbers |
| 3 | +title: Special Numbers |
| 4 | +sidebar_label: Special-Numbers |
| 5 | +tags: |
| 6 | + - Mathematical |
| 7 | + - Algorithms |
| 8 | +description: "This tutorial covers the solution to the Special Numbers problem from the GeeksforGeeks." |
| 9 | +--- |
| 10 | +## Problem Description |
| 11 | + |
| 12 | +A number is a special number if it’s digits only consist `0`, `1`, `2`, `3`, `4` or `5`. Given a number N and we have to find N-th Special Number. |
| 13 | + |
| 14 | +## Examples |
| 15 | + |
| 16 | +**Example 1:** |
| 17 | + |
| 18 | +``` |
| 19 | +Input: |
| 20 | +N = 6 |
| 21 | +Output: 5 |
| 22 | +Explanation: First 6 numbers are |
| 23 | +( 0, 1, 2, 3, 4, 5 ) |
| 24 | +``` |
| 25 | + |
| 26 | +**Example 2:** |
| 27 | + |
| 28 | +``` |
| 29 | +Input: |
| 30 | +N = 7 |
| 31 | +Output: 10 |
| 32 | +Explanation: First 7 numbers are |
| 33 | +( 0, 1, 2, 3, 4, 5, 10 ) |
| 34 | +``` |
| 35 | + |
| 36 | + |
| 37 | +Expected Time Complexity: O(logN) |
| 38 | + |
| 39 | +Expected Auxiliary Space: O(1) |
| 40 | + |
| 41 | +## Constraints |
| 42 | + |
| 43 | +* `1 ≤ N ≤ 10^5` |
| 44 | + |
| 45 | +## Problem Explanation |
| 46 | + |
| 47 | +The task is to traverse the number and count the digits. |
| 48 | + |
| 49 | +## Code Implementation |
| 50 | + |
| 51 | +### C++ Solution |
| 52 | + |
| 53 | +```cpp |
| 54 | +int findNthSpecial(int N) { |
| 55 | + int count = 0; |
| 56 | + int num = 0; |
| 57 | + while (true) { |
| 58 | + num++; |
| 59 | + if (isSpecial(num)) { |
| 60 | + count++; |
| 61 | + if (count == N) { |
| 62 | + return num; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +bool isSpecial(int num) { |
| 69 | + while (num > 0) { |
| 70 | + int digit = num % 10; |
| 71 | + if (digit > 5) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + num /= 10; |
| 75 | + } |
| 76 | + return true; |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +``` |
| 82 | +
|
| 83 | +```java |
| 84 | +public int findNthSpecial(int N) { |
| 85 | + int count = 0; |
| 86 | + int num = 0; |
| 87 | + while (true) { |
| 88 | + num++; |
| 89 | + if (isSpecial(num)) { |
| 90 | + count++; |
| 91 | + if (count == N) { |
| 92 | + return num; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | +
|
| 98 | +public boolean isSpecial(int num) { |
| 99 | + while (num > 0) { |
| 100 | + int digit = num % 10; |
| 101 | + if (digit > 5) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + num /= 10; |
| 105 | + } |
| 106 | + return true; |
| 107 | +} |
| 108 | +
|
| 109 | +
|
| 110 | +``` |
| 111 | + |
| 112 | +```python |
| 113 | + |
| 114 | +def find_nth_special(N): |
| 115 | + count = 0 |
| 116 | + num = 0 |
| 117 | + while True: |
| 118 | + num += 1 |
| 119 | + if is_special(num): |
| 120 | + count += 1 |
| 121 | + if count == N: |
| 122 | + return num |
| 123 | + |
| 124 | +def is_special(num): |
| 125 | + while num > 0: |
| 126 | + digit = num % 10 |
| 127 | + if digit > 5: |
| 128 | + return False |
| 129 | + num //= 10 |
| 130 | + return True |
| 131 | + |
| 132 | + |
| 133 | +``` |
| 134 | + |
| 135 | +```javascript |
| 136 | +function findNthSpecial(N) { |
| 137 | + let count = 0; |
| 138 | + let num = 0; |
| 139 | + while (true) { |
| 140 | + num++; |
| 141 | + if (isSpecial(num)) { |
| 142 | + count++; |
| 143 | + if (count === N) { |
| 144 | + return num; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +function isSpecial(num) { |
| 151 | + while (num > 0) { |
| 152 | + const digit = num % 10; |
| 153 | + if (digit > 5) { |
| 154 | + return false; |
| 155 | + } |
| 156 | + num = Math.floor(num / 10); |
| 157 | + } |
| 158 | + return true; |
| 159 | +} |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | +``` |
| 164 | + |
| 165 | +## Solution Logic: |
| 166 | +1. Initialize a counter count to 0 and a number num to 0. |
| 167 | +2. Iterate through numbers starting from 1. |
| 168 | +3. For each number, check if it's special (i.e., its digits only consist of 0, 1, 2, 3, 4, or 5) using the isSpecial function. |
| 169 | +4. If the number is special, increment the count. |
| 170 | +5. If the count reaches N, return the current number. |
| 171 | +6. Repeat steps 2-5 until the N-th special number is found. |
| 172 | + |
| 173 | +## Time Complexity |
| 174 | + |
| 175 | +- The `isSpecial` function has a time complexity of O(log num), where num is the input number, because it iterates through the digits of the number. |
| 176 | + |
| 177 | +- The main function findNthSpecial has a time complexity of O(N log M), where N is the input number and M is the N-th special number, because it iterates through numbers and calls the isSpecial function for each number. |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | +## Space Complexity |
| 182 | + |
| 183 | +- The solution uses a constant amount of space to store the count and num variables, so the space complexity is O(1). |
0 commit comments