Rotated Digits

An integer x is 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:

  • 0, 1, and 8 rotate to themselves.
  • 2 and 5 rotate to each other; in this case they are rotated in a different direction, so 2 or 5 gets mirrored.
  • 6 and 9 rotate to each other.
  • The rest of the digits do not rotate to any other digit and become invalid.

Given an integer n, return the number of good integers in the range [1, n].

Example 1
Inputn = 10
Output4
There are four good numbers in the range [1, 10]: 2, 5, 6, and 9; 1 and 10 are not good because they remain unchanged after rotating.
Example 2
Inputn = 1
Output0
The only number in the range [1, 1] is 1, which remains unchanged after rotating, so there are no good numbers.

Constraints

  • 1 <= n <= 10^4

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate