Count Numbers Changed by Valid Digit Rotation

Quick Overview

Count Numbers Changed by Valid Digit Rotation. Rotate every decimal digit individually using these rules:0,1,8 stay unchanged;2 and 5 exchange;6 and 9 exchange;3,4,7 are invalid.

Count Numbers Changed by Valid Digit Rotation

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Rotate every decimal digit individually using these rules:0,1,8 stay unchanged;2 and 5 exchange;6 and 9 exchange;3,4,7 are invalid. Do not reverse digit order. Count integers from 1 through n that remain valid and become a different number. ```cpp class Solution { public: int rotatedDigits(int n); }; ``` ### Examples **Example 1:** ```text n = 5 Output: 2 Explanation: Only2 and5 are valid and changed. ``` **Example 2:** ```text n = 15 Output: 6 Explanation: The qualifying numbers are2,5,6,9,12,15. ``` ### Constraints - The inclusive upper bound n lies in [1,10000]. ### Contract considerations Apply the stated mapping to every digit. ```hint Read the boundary condition Which digits certify a change, and which invalidate the number? ``` ### Boundary reminders Apply the stated mapping to every digit.

Overview: Count Numbers Changed by Valid Digit Rotation. Rotate every decimal digit individually using these rules:0,1,8 stay unchanged;2 and 5 exchange;6 and 9 exchange;3,4,7 are invalid.

Google logo
Google
Jun 29, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Rotate every decimal digit individually using these rules:0,1,8 stay unchanged;2 and 5 exchange;6 and 9 exchange;3,4,7 are invalid. Do not reverse digit order. Count integers from 1 through n that remain valid and become a different number.

class Solution {
public:
    int rotatedDigits(int n);
};

Examples

Example 1:

n = 5
Output: 2
Explanation: Only2 and5 are valid and changed.

Example 2:

n = 15
Output: 6
Explanation: The qualifying numbers are2,5,6,9,12,15.

Constraints

  • The inclusive upper bound n lies in [1,10000].

Contract considerations

Apply the stated mapping to every digit.

Boundary reminders

Apply the stated mapping to every digit.