Last Updated: 13 Apr, 2021

Special Numbers

Moderate
Asked in companies
OYOSAP LabsPayPal

Problem statement

You are given an integer, ‘MAXVAL’. Your task is to determine the total number of special numbers present in the range, 1 to ‘MAXVAL’.

Note:
A special number is a number, which when rotated 180 degrees, resembles some other number in the same range. Every digit of a special number must be a valid digit.

The digits, 0,1,6,8,9, when rotated 180 degrees, become 0,1,9,8,6 respectively. While the digits, 2,3,4,5,7, when rotated do not become any valid digit.
For example:
‘8196’, when rotated 180 degrees, will become ‘9618’. We can observe that all the digits of this number are valid. So, this is a special number.
Input Format:
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the T test cases follow. 

The first line of each test case contains a single integer, ‘MAXVAL’.
Output Format:
For each test case, print a single integer, denoting the total number of special numbers in the range, 1 to ‘MAXVAL’.

Output for each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= MAXVAL < 10^4

Time Limit: 1sec

Approaches

01 Approach

The approach is to loop through all the numbers in the given range one by one and check whether this number is a special number or not. We can check if a number is special or not, by rotating it. This can be achieved by reversing the string representation of the number and thereafter, replacing every ‘6’ by ‘9’ and vice versa. 

 

Steps:

 

  • Initialize a variable, say count = 0. This will store the total count of special numbers in the given range.
  • Now, run a loop from num = 1 to num <= maxVal, and do:
    • Define a variable, say numString to store the string representation of num.
    • Now, define a boolean variable, say specialNum, and set it to true.
    • Now, check for every character, a, in numString.
      • If a is either 2, 3, 4, 5, 7, make specialNum = false.
    • Now, if specialNum = true, do:
      • Store the length of numString in len.
      • Run a loop from i = 0 to i < len / 2, and swap characters at numString[i] and numString[len - 1 - i].
      • Run a loop from i = 0 to i < len, and
        • If numString[i] is ‘6’, make it ‘9’.
        • Else, if it is ‘9’, make it ‘6’.
      • Now, if the numeric value of numString is less than or equal to maxVal, and not the same as num, and the first character of numString is not ‘0’, increase the value of count by 1.
  • Finally, return the value of count.

02 Approach

The approach is to observe the fact that any special number will only have special digits in it. So, instead of checking all the numbers in the range, 1 to ‘MAXVAL’ independently, we can only check numbers that are made up of special digits only. For any special number, we can get more special numbers by appending one of the digits from 0,1,6,8,9 one by one at the end of the special number and check for the new number. The process of checking whether a number is special or not has been discussed in the other approach.

 

Steps:

 

  1. Initialize a variable, say count=0. This will store the total count of special numbers in the given range.
  2. Call the function, checkNumbers(0,maxVal,count).
  3. Return the value of the count.

 

void checkNumbers(num , maxVal , count) {

 

  • If the value of num is more than maxVal, return.
  • Define a variable, say numString, to store the rotated string representation of num. Formally, make numString = rotate(num).
  • Now, if the numeric value of numString is less than or equal to maxVal, and not the same as num, and the first character of numString is not ‘0’, increase the value of count by 1.
  • Now, if num>0, call checkNumbers(num*10,maxVal,count). We call this function, only if num>0, because if we call it for num=0, it will form infinite loop.
  • Call checkNumbers(num*10+1,maxVal,count).
  • Call checkNumbers(num*10+6,maxVal,count).
  • Call checkNumbers(num*10+8,maxVal,count).
  • Call checkNumbers(num*10+9,maxVal,count).

 

String rotate(num) {

 

  • Define a variable, say numString to store the string representation of num.
  • Store the length of numString in len.
  • Run a loop from i=0 to i<len/2, and swap characters at numString[i] and numString[len-1-i].
  • Run a loop from i=0 to i<len, and
    • If numString[i] is ‘6’, make it ‘9’.
    • Else, if it is ‘9’, make it ‘6’.
  • Return, numString.