|
| 1 | +--- |
| 2 | + |
| 3 | +id: how-many-xs |
| 4 | +title: How Many Xs Solution |
| 5 | +sidebar_label: 0004 - How Many Xs |
| 6 | +tags: |
| 7 | + - Number Theory |
| 8 | + - Counting |
| 9 | + - Mathematics |
| 10 | + - Python |
| 11 | + - JavaScript |
| 12 | + - TypeScript |
| 13 | + - Java |
| 14 | + - C++ |
| 15 | +description: "This is a solution to the How Many Xs problem." |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +In this page, we will solve the How Many Xs problem using different approaches: iterative and mathematical. We will provide the implementation of the solution in JavaScript, TypeScript, Python, Java, and C++. |
| 20 | + |
| 21 | +## Problem Description |
| 22 | + |
| 23 | +You are given an integer `L`, `R`, and `X`. Find the number of occurrences of `X `in all the numbers in the range `(L, R)` excluding `L` and `R`. |
| 24 | +### Examples |
| 25 | + |
| 26 | +**Example 1:** |
| 27 | + |
| 28 | +```plaintext |
| 29 | +Input: L = 10, R = 20, X = 1 |
| 30 | +Output: 10 |
| 31 | +Explanation: The digit 1 appears 11 times in numbers from 10 to 19. |
| 32 | +``` |
| 33 | + |
| 34 | +**Example 2:** |
| 35 | + |
| 36 | +```plaintext |
| 37 | +Input: L = 100, R = 120, X = 2 |
| 38 | +Output: 2 |
| 39 | +Explanation: The digit 2 appears 3 times in numbers from 100 to 119. |
| 40 | +``` |
| 41 | + |
| 42 | +### Constraints |
| 43 | + |
| 44 | +- `0 <= L <= R <= 10^9` |
| 45 | +- `0 <= X <= 9` |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +## Solution for How Many Xs Problem |
| 50 | + |
| 51 | +### Intuition and Approach |
| 52 | + |
| 53 | +The problem can be solved using different approaches such as iterative counting and mathematical properties. |
| 54 | + |
| 55 | +<Tabs> |
| 56 | + <tabItem value="Iterative" label="Iterative"> |
| 57 | + |
| 58 | +### Approach 1: Iterative |
| 59 | + |
| 60 | +The iterative approach involves iterating through each number in the range and counting the occurrences of the digit `X`. |
| 61 | + |
| 62 | +#### Implementation |
| 63 | + |
| 64 | +```jsx live |
| 65 | +function countXs() { |
| 66 | + const L = 10; |
| 67 | + const R = 20; |
| 68 | + const X = 1; |
| 69 | + |
| 70 | + const countDigitOccurrences = (L, R, X) => { |
| 71 | + let count = 0; |
| 72 | + for (let i = L+1; i < R; i++) { |
| 73 | + let num = i; |
| 74 | + while (num > 0) { |
| 75 | + if (num % 10 === X) count++; |
| 76 | + num = Math.floor(num / 10); |
| 77 | + } |
| 78 | + } |
| 79 | + return count; |
| 80 | + }; |
| 81 | + |
| 82 | + const result = countDigitOccurrences(L, R, X); |
| 83 | + return ( |
| 84 | + <div> |
| 85 | + <p> |
| 86 | + <b>Input:</b> L = {L}, R = {R}, X = {X} |
| 87 | + </p> |
| 88 | + <p> |
| 89 | + <b>Output:</b> {result} |
| 90 | + </p> |
| 91 | + </div> |
| 92 | + ); |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +#### Codes in Different Languages |
| 97 | + |
| 98 | +<Tabs> |
| 99 | + <TabItem value="JavaScript" label="JavaScript" default> |
| 100 | + <SolutionAuthor name="@manishh12"/> |
| 101 | + ```javascript |
| 102 | + function countDigitOccurrences(L, R, X) { |
| 103 | + let count = 0; |
| 104 | + for (let i = L+1; i < R; i++) { |
| 105 | + let num = i; |
| 106 | + while (num > 0) { |
| 107 | + if (num % 10 === X) count++; |
| 108 | + num = Math.floor(num / 10); |
| 109 | + } |
| 110 | + } |
| 111 | + return count; |
| 112 | + } |
| 113 | + ``` |
| 114 | + |
| 115 | + </TabItem> |
| 116 | + <TabItem value="TypeScript" label="TypeScript"> |
| 117 | + <SolutionAuthor name="@manishh12"/> |
| 118 | + ```typescript |
| 119 | + function countDigitOccurrences(L: number, R: number, X: number): number { |
| 120 | + let count = 0; |
| 121 | + for (let i = L+1; i < R; i++) { |
| 122 | + let num = i; |
| 123 | + while (num > 0) { |
| 124 | + if (num % 10 === X) count++; |
| 125 | + num = Math.floor(num / 10); |
| 126 | + } |
| 127 | + } |
| 128 | + return count; |
| 129 | + } |
| 130 | + ``` |
| 131 | + |
| 132 | + </TabItem> |
| 133 | + <TabItem value="Python" label="Python"> |
| 134 | + <SolutionAuthor name="@manishh12"/> |
| 135 | + ```python |
| 136 | + def count_digit_occurrences(L, R, X): |
| 137 | + count = 0 |
| 138 | + for i in range(L+1, R): |
| 139 | + num = i |
| 140 | + while num > 0: |
| 141 | + if num % 10 == X: |
| 142 | + count += 1 |
| 143 | + num //= 10 |
| 144 | + return count |
| 145 | + ``` |
| 146 | + |
| 147 | + </TabItem> |
| 148 | + <TabItem value="Java" label="Java"> |
| 149 | + <SolutionAuthor name="@manishh12"/> |
| 150 | + ```java |
| 151 | + class Solution { |
| 152 | + public int countDigitOccurrences(int L, int R, int X) { |
| 153 | + int count = 0; |
| 154 | + for (int i = L+1; i < R; i++) { |
| 155 | + int num = i; |
| 156 | + while (num > 0) { |
| 157 | + if (num % 10 == X) { |
| 158 | + count++; |
| 159 | + } |
| 160 | + num /= 10; |
| 161 | + } |
| 162 | + } |
| 163 | + return count; |
| 164 | + } |
| 165 | + } |
| 166 | + ``` |
| 167 | + |
| 168 | + </TabItem> |
| 169 | + <TabItem value="C++" label="C++"> |
| 170 | + <SolutionAuthor name="@manishh12"/> |
| 171 | + ```cpp |
| 172 | + #include <iostream> |
| 173 | +
|
| 174 | + using namespace std; |
| 175 | +
|
| 176 | + int countDigitOccurrences(int L, int R, int X) { |
| 177 | + int count = 0; |
| 178 | + for (int i = L+1; i < R; i++) { |
| 179 | + int num = i; |
| 180 | + while (num > 0) { |
| 181 | + if (num % 10 == X) { |
| 182 | + count++; |
| 183 | + } |
| 184 | + num /= 10; |
| 185 | + } |
| 186 | + } |
| 187 | + return count; |
| 188 | + } |
| 189 | + ``` |
| 190 | + |
| 191 | + </TabItem> |
| 192 | +</Tabs> |
| 193 | + |
| 194 | +#### Complexity Analysis |
| 195 | + |
| 196 | +- Time Complexity: $$O((R - L)*logR)$$ |
| 197 | +- Space Complexity: $$O(1)$$ |
| 198 | + |
| 199 | +</tabItem> |
| 200 | +<tabItem value="Mathematical" label="Mathematical"> |
| 201 | + |
| 202 | +### Approach 2: Mathematical |
| 203 | + |
| 204 | +The mathematical approach involves counting the occurrences of the digit `X` in the range without iterating through each number. |
| 205 | + |
| 206 | +#### Implementation |
| 207 | + |
| 208 | +```jsx live |
| 209 | +function countXs() { |
| 210 | + const L = 10; |
| 211 | + const R = 20; |
| 212 | + const X = 1; |
| 213 | +
|
| 214 | + const countOccurrences = (num, X) => { |
| 215 | + let count = 0, factor = 1, nextNum = 0, currentNum = 0; |
| 216 | + while (num / factor > 0) { |
| 217 | + currentNum = Math.floor((num / factor) % 10); |
| 218 | + nextNum = Math.floor(num / (factor * 10)); |
| 219 | + count += nextNum * factor; |
| 220 | + if (currentNum > X) count += factor; |
| 221 | + else if (currentNum === X) count += num % factor + 1; |
| 222 | + factor *= 10; |
| 223 | + } |
| 224 | + return count; |
| 225 | + }; |
| 226 | +
|
| 227 | + const countX = (L, R, X) => { |
| 228 | + return countOccurrences(R - 1, X) - countOccurrences(L, X); |
| 229 | + }; |
| 230 | +
|
| 231 | + const result = countX(L, R, X); |
| 232 | + return ( |
| 233 | + <div> |
| 234 | + <p> |
| 235 | + <b>Input:</b> L = {L}, R = {R}, X = {X} |
| 236 | + </p> |
| 237 | + <p> |
| 238 | + <b>Output:</b> {result} |
| 239 | + </p> |
| 240 | + </div> |
| 241 | + ); |
| 242 | +} |
| 243 | +
|
| 244 | +``` |
| 245 | + |
| 246 | +#### Code in Different Languages |
| 247 | + |
| 248 | +<Tabs> |
| 249 | + <TabItem value="JavaScript" label="JavaScript" default> |
| 250 | + <SolutionAuthor name="@manishh12"/> |
| 251 | + ```javascript |
| 252 | + function countDigitOccurrences(num, X) { |
| 253 | + let count = 0, factor = 1, nextNum = 0, currentNum = 0; |
| 254 | + while (num / factor > 0) { |
| 255 | + currentNum = Math.floor((num / factor) % 10); |
| 256 | + nextNum = Math.floor(num / (factor * 10)); |
| 257 | + count += nextNum * factor; |
| 258 | + if (currentNum > X) count += factor; |
| 259 | + else if (currentNum === X) count += num % factor + 1; |
| 260 | + factor *= 10; |
| 261 | + } |
| 262 | + return count; |
| 263 | + } |
| 264 | +
|
| 265 | + function countX(L, R, X) { |
| 266 | + return countDigitOccurrences(R - 1, X) - countDigitOccurrences(L, X); |
| 267 | + } |
| 268 | + ``` |
| 269 | + |
| 270 | + </TabItem> |
| 271 | + <TabItem value="TypeScript" label="TypeScript"> |
| 272 | + <SolutionAuthor name="@manishh12"/> |
| 273 | + ```typescript |
| 274 | + function countDigitOccurrences(num: number, X: number): number { |
| 275 | + let count = 0, factor = 1, nextNum = 0, currentNum = 0; |
| 276 | + while (num / factor > 0) { |
| 277 | + currentNum = Math.floor((num / factor) % 10); |
| 278 | + nextNum = Math.floor(num / (factor * 10)); |
| 279 | + count += nextNum * factor; |
| 280 | + if (currentNum > X) count += factor; |
| 281 | + else if (currentNum === X) count += num % factor + 1; |
| 282 | + factor *= 10; |
| 283 | + } |
| 284 | + return count; |
| 285 | + } |
| 286 | +
|
| 287 | + function countX(L: number, R: number, X: number): number { |
| 288 | + return countDigitOccurrences(R - 1, X) - countDigitOccurrences(L, X); |
| 289 | + } |
| 290 | + ``` |
| 291 | + |
| 292 | + </TabItem> |
| 293 | + <TabItem value="Python" label="Python"> |
| 294 | + <SolutionAuthor name="@manishh12"/> |
| 295 | + ```python |
| 296 | + def count_digit_occurrences(num, X): |
| 297 | + count = 0 |
| 298 | + factor = 1 |
| 299 | + while num // factor > 0: |
| 300 | + current_num = (num // factor) % 10 |
| 301 | + next_num = num // (factor * 10) |
| 302 | + count |
| 303 | +
|
| 304 | + += next_num * factor |
| 305 | + if current_num > X: |
| 306 | + count += factor |
| 307 | + elif current_num == X: |
| 308 | + count += num % factor + 1 |
| 309 | + factor *= 10 |
| 310 | + return count |
| 311 | +
|
| 312 | + def count_X(L, R, X): |
| 313 | + return count_digit_occurrences(R - 1, X) - count_digit_occurrences(L , X) |
| 314 | + ``` |
| 315 | + |
| 316 | + </TabItem> |
| 317 | + <TabItem value="Java" label="Java"> |
| 318 | + <SolutionAuthor name="@manishh12"/> |
| 319 | + ```java |
| 320 | + class Solution { |
| 321 | + public int countDigitOccurrences(int num, int X) { |
| 322 | + int count = 0, factor = 1, nextNum = 0, currentNum = 0; |
| 323 | + while (num / factor > 0) { |
| 324 | + currentNum = (num / factor) % 10; |
| 325 | + nextNum = num / (factor * 10); |
| 326 | + count += nextNum * factor; |
| 327 | + if (currentNum > X) { |
| 328 | + count += factor; |
| 329 | + } else if (currentNum == X) { |
| 330 | + count += num % factor + 1; |
| 331 | + } |
| 332 | + factor *= 10; |
| 333 | + } |
| 334 | + return count; |
| 335 | + } |
| 336 | +
|
| 337 | + public int countX(int L, int R, int X) { |
| 338 | + return countDigitOccurrences(R - 1, X) - countDigitOccurrences(L , X); |
| 339 | + } |
| 340 | + } |
| 341 | + ``` |
| 342 | + |
| 343 | + </TabItem> |
| 344 | + <TabItem value="C++" label="C++"> |
| 345 | + <SolutionAuthor name="@manishh12"/> |
| 346 | + ```cpp |
| 347 | + #include <iostream> |
| 348 | +
|
| 349 | + using namespace std; |
| 350 | +
|
| 351 | + int countDigitOccurrences(int num, int X) { |
| 352 | + int count = 0, factor = 1, nextNum = 0, currentNum = 0; |
| 353 | + while (num / factor > 0) { |
| 354 | + currentNum = (num / factor) % 10; |
| 355 | + nextNum = num / (factor * 10); |
| 356 | + count += nextNum * factor; |
| 357 | + if (currentNum > X) { |
| 358 | + count += factor; |
| 359 | + } else if (currentNum == X) { |
| 360 | + count += num % factor + 1; |
| 361 | + } |
| 362 | + factor *= 10; |
| 363 | + } |
| 364 | + return count; |
| 365 | + } |
| 366 | +
|
| 367 | + int countX(int L, int R, int X) { |
| 368 | + return countDigitOccurrences(R - 1, X) - countDigitOccurrences(L , X); |
| 369 | + } |
| 370 | + ``` |
| 371 | + |
| 372 | + </TabItem> |
| 373 | +</Tabs> |
| 374 | + |
| 375 | +#### Complexity Analysis |
| 376 | + |
| 377 | +- Time Complexity: $$O(\log_{10}(R))$$ |
| 378 | +- Space Complexity: $$O(1)$$ |
| 379 | + |
| 380 | +</tabItem> |
| 381 | +</Tabs> |
| 382 | + |
| 383 | +:::tip |
| 384 | +The mathematical approach is more efficient for large ranges. Choose the method that best suits the given constraints. |
| 385 | +::: |
| 386 | +## References |
| 387 | + |
| 388 | +- **GeeksforGeeks Problem:** [GeeksforGeeks Problem](https://www.geeksforgeeks.org/problems/how-many-xs4514/0) |
| 389 | +- **Solution Link:** [How Many X's Solution on GeeksforGeeks](https://www.geeksforgeeks.org/problems/how-many-xs4514/0) |
| 390 | +- **Authors GeeksforGeeks Profile:** [Manish Kumar Gupta](https://www.geeksforgeeks.org/user/manishd5hla) |
| 391 | +
|
| 392 | +--- |
0 commit comments