Last active
October 24, 2025 16:15
-
-
Save tatsuyax25/a1efb444ac2ca7284deafc091337c609 to your computer and use it in GitHub Desktop.
An integer x is numerically balanced if for every digit d in the number x, there are exactly d occurrences of that digit in x. Given an integer n, return the smallest numerically balanced number strictly greater than n.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Finds the next "beautiful" number greater than the given input. | |
| * A beautiful number is defined as one where each digit appears exactly as many times as its value. | |
| * For example, 22 is beautiful because digit 2 appears twice. | |
| * | |
| * @param {number} n - The input number. | |
| * @return {number} - The next beautiful number greater than n. | |
| */ | |
| var nextBeautifulNumber = function(n) { | |
| // Precomputed list of beautiful numbers in ascending order. | |
| // These are numbers where each digit appears exactly as many times as its value. | |
| const balancedNums = [ | |
| 1, 22, 122, 212, 221, 333, 1333, 3133, 3313, 3331, 4444, 14444, | |
| 22333, 23233, 23323, 23332, 32233, 32323, 32332, 33223, 33232, | |
| 33322, 41444, 44144, 44414, 44441, 55555, 122333, 123233, 123323, | |
| 123332, 132233, 132323, 132332, 133223, 133232, 133322, 155555, | |
| 212333, 213233, 213323, 213332, 221333, 223133, 223313, 223331, | |
| 224444, 231233, 231323, 231332, 232133, 232313, 232331, 233123, | |
| 233132, 233213, 233231, 233312, 233321, 242444, 244244, 244424, | |
| 244442, 312233, 312323, 312332, 313223, 313232, 313322, 321233, | |
| 321323, 321332, 322133, 322313, 322331, 323123, 323132, 323213, | |
| 323231, 323312, 323321, 331223, 331232, 331322, 332123, 332132, | |
| 332213, 332231, 332312, 332321, 333122, 333212, 333221, 422444, | |
| 424244, 424424, 424442, 442244, 442424, 442442, 444224, 444242, | |
| 444422, 515555, 551555, 555155, 555515, 555551, 666666, 1224444 | |
| ]; | |
| // Iterate through the list and return the first number greater than n | |
| for (let i = 0; i < balancedNums.length; i++) { | |
| if (balancedNums[i] > n) { | |
| return balancedNums[i]; | |
| } | |
| } | |
| // If no number is found (unlikely with current list), return undefined | |
| return undefined; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment