788. Rotated Digits
Difficulty: Medium
Topics: Senior, Math, Dynamic Programming, Weekly Contest 73
An integer x is a good if after rotating each digit individually by 180 degrees, we get a valid number that is different from x. Each digit must be rotated - we cannot choose to leave it alone.
A number is valid if each digit remains a digit after rotation. For example:
-
0,1, and8rotate to themselves, -
2and5rotate to each other (in this case they are rotated in a different direction, in other words,2or5gets mirrored), -
6and9rotate to each other, and - the rest of the numbers do not rotate to any other number and become invalid.
Given an integer n, return the number of good integers in the range [1, n].
Example 1:
- Input: n = 10
- Output: 4
-
Explanation:
- There are four good numbers in the range [1, 10] : 2, 5, 6, 9.
- Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.
Example 2:
- Input: n = 1
- Output: 0
Example 3:
- Input: n = 2
- Output: 1
Example 4:
- Input: n = 100
- Output: 40
Example 5:
- Input: n = 30
- Output: 13
Example 5:
- Input: n = 10000
- Output: 2320
Constraints:
1 <= n <= 10⁴
Solution:
The solution counts integers between 1 and n that are good — meaning when each digit is rotated 180°, the resulting number is different from the original and contains no invalid digits (3, 4, 7).
It iterates over each number, checks digit-by-digit using the rotation rules, and increments a counter for good numbers.
Approach:
- Loop through all integers from
1ton. - For each number, check if it is “good” using digit-by-digit validation.
- A number is not good if:
- Any digit is
3,4, or7(invalid). - After rotation, the number remains unchanged (i.e., contains only
0,1, or8).
- Any digit is
- A number is good if:
- No invalid digits.
- At least one digit from
{2,5,6,9}exists (since those change on rotation).
- Return the total count.
Let's implement this solution in PHP: 788. Rotated Digits
<?php
/**
* @param Integer $n
* @return Integer
*/
function rotatedDigits(int $n): int
{
...
...
...
/**
* go to ./solution.php
*/
}
/**
* Check if a number is "good"
*
* @param Integer $num
* @return bool
*/
function isGood(int $num): bool
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo rotatedDigits(10) . "\n"; // Output: 4
echo rotatedDigits(1) . "\n"; // Output: 0
echo rotatedDigits(2) . "\n"; // Output: 1
echo rotatedDigits(100) . "\n"; // Output: 40
echo rotatedDigits(30) . "\n"; // Output: 13
echo rotatedDigits(10000) . "\n"; // Output: 2320
?>
Explanation:
-
Digit rotation mapping:
-
0,1,8→ rotate to themselves. -
2,5→ rotate to each other. -
6,9→ rotate to each other. -
3,4,7→ become invalid.
-
-
Why
hasRotatingflag?- If all digits are in
{0,1,8}, the rotated number equals the original → not good. - If any digit is in
{2,5,6,9}, the rotated number is different → good.
- If all digits are in
-
Early exit: If an invalid digit
3,4,7is found, the number is immediately rejected.
Complexity
-
Time Complexity: O(n * d), where
dis the number of digits inn(at most 5 forn ≤ 10⁴). So roughly O(n log₁₀ n). - Space Complexity: O(1) — only a few variables used per iteration.
-
Trade-off: Simple implementation but linear in
n, which is fine given the constraintn ≤ 10⁴.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)