|
| 1 | +# Brute Force with Validation | 35 Lines | O(k × d) | 247ms |
| 2 | + |
| 3 | +# Intuition |
| 4 | +A numerically balanced number has a very specific property: each digit d must appear exactly d times. These numbers are rare, so we can use brute force to check each candidate starting from n+1 until we find one that satisfies the balanced property. |
| 5 | + |
| 6 | +# Approach |
| 7 | +**Sequential Search with Digit Frequency Validation:** |
| 8 | +- Iterate through numbers starting from n+1 |
| 9 | +- For each candidate, count digit frequencies and validate the balanced property |
| 10 | +- Return the first number that satisfies all conditions |
| 11 | + |
| 12 | +**Step-by-Step Process:** |
| 13 | + |
| 14 | +1. **Helper Function: isNumericallyBalanced(num)** |
| 15 | + |
| 16 | + **Count Digit Frequencies:** |
| 17 | + - Create frequency array of size 10 (for digits 0-9) |
| 18 | + - Extract digits using modulo and division: `digit = num % 10` |
| 19 | + - Repeatedly divide by 10 until num becomes 0 |
| 20 | + - Count each digit's occurrences |
| 21 | + |
| 22 | + **Validate Balanced Property:** |
| 23 | + - For each digit d from 0 to 9: |
| 24 | + - If digit d appears in the number (frequency > 0) |
| 25 | + - It must appear exactly d times (frequency == d) |
| 26 | + - If frequency ≠ d, return false |
| 27 | + - Special case: digit 0 cannot appear (would require 0 occurrences, but we counted it) |
| 28 | + |
| 29 | + **Return true if all checks pass** |
| 30 | + |
| 31 | +2. **Main Function: nextBeautifulNumber(n)** |
| 32 | + |
| 33 | + **Set Upper Bound:** |
| 34 | + - Largest balanced number ≤ 10^6 is 1224444 |
| 35 | + - This has: one 1, two 2's, four 4's (valid!) |
| 36 | + - No need to search beyond this for given constraints |
| 37 | + |
| 38 | + **Sequential Search:** |
| 39 | + - Start from n+1 (strictly greater) |
| 40 | + - Check each candidate with isNumericallyBalanced |
| 41 | + - Return first valid number found |
| 42 | + |
| 43 | +3. **Why Brute Force is Acceptable:** |
| 44 | + - Balanced numbers are extremely rare |
| 45 | + - In range [1, 10^6], there are only about 80 balanced numbers |
| 46 | + - Average gap between consecutive balanced numbers is ~12,500 |
| 47 | + - Max iterations needed is much less than 10^6 |
| 48 | + |
| 49 | +**Numerically Balanced Number Properties:** |
| 50 | + |
| 51 | +- **Digit 0 cannot appear:** Would need 0 occurrences, contradiction |
| 52 | +- **Digit 1 can appear once:** 1 × 1 = 1 digit total |
| 53 | +- **Digit 2 must appear twice:** 2 × 2 = 4 digits total (including both 2's) |
| 54 | +- **Valid combinations are limited:** e.g., {1,2,2}, {3,3,3}, {1,3,3,3}, {1,2,2,4,4,4,4} |
| 55 | + |
| 56 | +**Example Balanced Numbers:** |
| 57 | +- 1, 22, 122, 212, 221, 333, 1333, 3133, 3313, 3331, ... |
| 58 | +- 1224444 (one 1, two 2's, four 4's) |
| 59 | + |
| 60 | +**Example Walkthrough (n = 1000):** |
| 61 | +- Try 1001: has 1(×2), 0(×2) → 0 appears but shouldn't → invalid |
| 62 | +- Try 1002, 1003, ..., 1332: various violations |
| 63 | +- Try 1333: |
| 64 | + - Frequency: 1(×1), 3(×3) |
| 65 | + - Check: 1 appears 1 time ✓, 3 appears 3 times ✓ |
| 66 | + - Valid! Return 1333 |
| 67 | + |
| 68 | +**Optimization Opportunities (not implemented):** |
| 69 | +- Could precompute all balanced numbers and binary search |
| 70 | +- Could generate candidates intelligently rather than checking all numbers |
| 71 | +- For this problem, brute force is simple and fast enough |
| 72 | + |
| 73 | +# Complexity |
| 74 | +- Time complexity: $$O(k \times d)$$ where k is the gap to next balanced number (avg ~12,500) and d is number of digits (~6-7) |
| 75 | +- Space complexity: $$O(1)$$ - only using fixed-size frequency array |
| 76 | + |
| 77 | +# Code |
| 78 | +```typescript |
| 79 | +const isNumericallyBalanced = (num: number): boolean => { |
| 80 | + const digitFrequency = new Array(10).fill(0); |
| 81 | + let remainingNumber = num; |
| 82 | + |
| 83 | + while (remainingNumber > 0) { |
| 84 | + const digit = remainingNumber % 10; |
| 85 | + digitFrequency[digit]++; |
| 86 | + remainingNumber = Math.floor(remainingNumber / 10); |
| 87 | + } |
| 88 | + |
| 89 | + for (let digit = 0; digit < 10; digit++) { |
| 90 | + const frequency = digitFrequency[digit]; |
| 91 | + |
| 92 | + if (frequency > 0 && frequency !== digit) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return true; |
| 98 | +}; |
| 99 | + |
| 100 | +const nextBeautifulNumber = (n: number): number => { |
| 101 | + const MAX_BALANCED_NUMBER = 1224444; |
| 102 | + |
| 103 | + for (let candidate = n + 1; candidate <= MAX_BALANCED_NUMBER; candidate++) { |
| 104 | + if (isNumericallyBalanced(candidate)) { |
| 105 | + return candidate; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return -1; |
| 110 | +}; |
| 111 | +``` |
0 commit comments