Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
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, andGiven 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
Constraints:
1 <= n <= 104Problem summary: 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].
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Dynamic Programming
10
1
2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #788: Rotated Digits
class Solution {
private int[] d = new int[] {0, 1, 5, -1, -1, 2, 9, -1, 8, 6};
public int rotatedDigits(int n) {
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (check(i)) {
++ans;
}
}
return ans;
}
private boolean check(int x) {
int y = 0, t = x;
int k = 1;
while (t > 0) {
int v = t % 10;
if (d[v] == -1) {
return false;
}
y = d[v] * k + y;
k *= 10;
t /= 10;
}
return x != y;
}
}
// Accepted solution for LeetCode #788: Rotated Digits
func rotatedDigits(n int) int {
d := []int{0, 1, 5, -1, -1, 2, 9, -1, 8, 6}
check := func(x int) bool {
y, t := 0, x
k := 1
for ; t > 0; t /= 10 {
v := t % 10
if d[v] == -1 {
return false
}
y = d[v]*k + y
k *= 10
}
return x != y
}
ans := 0
for i := 1; i <= n; i++ {
if check(i) {
ans++
}
}
return ans
}
# Accepted solution for LeetCode #788: Rotated Digits
class Solution:
def rotatedDigits(self, n: int) -> int:
def check(x):
y, t = 0, x
k = 1
while t:
v = t % 10
if d[v] == -1:
return False
y = d[v] * k + y
k *= 10
t //= 10
return x != y
d = [0, 1, 5, -1, -1, 2, 9, -1, 8, 6]
return sum(check(i) for i in range(1, n + 1))
// Accepted solution for LeetCode #788: Rotated Digits
struct Solution;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum D {
Invalid,
Same,
Different,
}
impl D {
fn new(d: usize) -> Self {
match d {
0 | 1 | 8 => D::Same,
2 | 5 | 6 | 9 => D::Different,
_ => D::Invalid,
}
}
}
impl Solution {
fn rotated_digits(n: i32) -> i32 {
let n: usize = n as usize;
let mut a: Vec<D> = vec![D::Invalid; (n + 1) as usize];
for i in 0..=n {
if i < 10 {
a[i] = D::new(i);
} else {
let j = i / 10;
let k = i % 10;
a[i] = match (a[j], a[k]) {
(D::Invalid, _) => D::Invalid,
(_, D::Invalid) => D::Invalid,
(D::Different, _) => D::Different,
(_, D::Different) => D::Different,
(D::Same, D::Same) => D::Same,
}
}
}
a.iter()
.fold(0, |sum, &d| if d == D::Different { sum + 1 } else { sum })
}
}
#[test]
fn test() {
assert_eq!(Solution::rotated_digits(10), 4);
}
// Accepted solution for LeetCode #788: Rotated Digits
function rotatedDigits(n: number): number {
const d: number[] = [0, 1, 5, -1, -1, 2, 9, -1, 8, 6];
const check = (x: number): boolean => {
let y = 0;
let t = x;
let k = 1;
while (t > 0) {
const v = t % 10;
if (d[v] === -1) {
return false;
}
y = d[v] * k + y;
k *= 10;
t = Math.floor(t / 10);
}
return x !== y;
};
return Array.from({ length: n }, (_, i) => i + 1).filter(check).length;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.