Reverse Number

Easy
0/40
Average time to solve is 10m
profile
Contributed by
82 upvotes
Asked in companies
Wells FargoProtiumErnst & Young (EY)

Problem statement

Ninja is feeling very bored and wants to try something new. So, he decides to find the reverse of a given number. But he cannot do it on his own and needs your help.

Note:

If a number has trailing zeros, then its reverse will not include them. For e.g., the reverse of 10400 will be 401 instead of 00401.
Detailed explanation ( Input/output format, Notes, Images )

Input Format:

The first line contains an integer 'T' which denotes the number of test cases or queries to be run.

The first line of each test case contains one integer ‘N’ denoting the given number.

Output Format:

For each case, you need to print the reverse of the given number.

The output of each test case will be printed on a separate line.

Note:

You do not need to input or print anything, it has already been taken care of. Just implement the given function.

Constraints:

1 <= T <= 5
1 <= N <= 10 ^ 18

Time Limit = 1 sec

Sample Input 1:

2
10400
12345

Sample Output 1:

401
54321

Explanation of Sample Input 1:

Test case 1:
For the first test case of sample output 1, as the number is ‘10400’, after finding the reverse, it turns out to be ‘00401’ but we need to leave the trailing zeros. Therefore our solution is 401

Test case 2:   
For the second test case of sample output 1, as the number is ‘12345’, the reverse will be ‘54321’

Sample Input 2:

2
1000
7654321

Sample Output 2:

1
1234567

Explanation of Sample Input 2:

Test case 1:
For the first test case of sample output 2, as the input number is ‘1000’, we get the reverse as ‘1’.

Test case 2:   
For the second test case of sample output 2, as the input number is ‘7654321’, we get the reverse as ‘1234567’.
Hint

Use basic mathematics properties.

Approaches (1)
Number Manipulations

Firstly to solve the problem of trailing zeros, we can run a while loop that shall divide the number by 10 and the condition for the while loop shall be that the modules of the number by 10 should be 0. Once we are done with this, we will run a while loop for reversing the number. The loop will be functional till the time n is greater than 0. Inside the loop, we will be taking out the last digit of the number using the modulus operator and appending it to a new answer variable which is declared outside the loop. After this, the number will be divided by 10 as we do not need the later digit anymore.

 

Algorithm:

 

  • Declare a variable ‘reverseNum’ and initialize it with 0
  • Declare a variable ‘remainder' and initialize it with 0
  • Run a while loop till ‘n’ % 10 is equal to 0 to remove the trailing zeros
    • Divide the number by 10 to remove the last digit.
  • Run a while loop till 'n' is greater than 0
    • Calculate the last digit of the number by using n % 10.
    • Multiply 'reverseNum' by 10 to create a new digit towards the end and add the remainder to it.
    • Divide the number by 10 to remove the last digit.
  • Return the 'reverseNum'
Time Complexity

O(log N) (base 10) where N is the given input.

 

As we are only dealing with the digits of the given number, we can simply perform modules of the number and extract the digits.

Space Complexity

O(1)

 

We need constant space. So space complexity is O(1).

Code Solution
(100% EXP penalty)
Reverse Number
Full screen
Console