DEV Community

Cover image for 788. Rotated Digits
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on

788. Rotated Digits

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, and 8 rotate to themselves,
  • 2 and 5 rotate to each other (in this case they are rotated in a different direction, in other words, 2 or 5 gets mirrored),
  • 6 and 9 rotate 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 1 to n.
  • For each number, check if it is “good” using digit-by-digit validation.
  • A number is not good if:
    • Any digit is 3, 4, or 7 (invalid).
    • After rotation, the number remains unchanged (i.e., contains only 0, 1, or 8).
  • 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
?>
Enter fullscreen mode Exit fullscreen mode

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 hasRotating flag?
    • 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.
  • Early exit: If an invalid digit 3,4,7 is found, the number is immediately rejected.

Complexity

  • Time Complexity: O(n * d), where d is the number of digits in n (at most 5 for n ≤ 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 constraint n ≤ 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!
Buy Me A Coffee

If you want more helpful content like this, feel free to follow me:

Top comments (1)

Collapse
 
xiaoming_nian_94953c8c9b8 profile image
Andy Nian

The solution that checks each number from 1 to n seems tedious. Is there a way to optimize this using dynamic programming or bit manipulation to cut down on iteration time? I've been using prachub.com for practice, and their question bank is helpful for spotting patterns like this. Their question sets often include the edge cases that recruiters like to use. Mixing in that prep might make these problems less of a slog to solve.