Print even from square by reversing the digits

Easy
0/40
0 upvote

Problem statement

You are given an integer 'n'. Your task is to perform a sequence of operations:

1)Reverse the digits of 'n'.
2)Square the reversed number.
3)From the squared result, extract only the even digits (0, 2, 4, 6, 8) and print them in the order they appear.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains a single integer 'n'.


Output Format:
Print the even digits found in the final squared number, separated by spaces.

If no even digits are found in the squared result, print nothing (an empty line).


Note:
When reversing the number, any leading zeros that result should be dropped. For example, reversing 120 results in 21, not 021.

The squared number can be large, so ensure you use a data type that can accommodate it (e.g., a 64-bit integer like long in Java or long long in C++).
Sample Input 1:
24


Sample Output 1:
6 4


Explanation for Sample 1:
1. The given number is n = 24.
2. Reversing the digits of 24 gives 42.
3. Squaring the reversed number: 42 * 42 = 1764.
4. The even digits in 1764 are 6 and 4.


Sample Input 2:
120


Sample Output 2:
4 4


Explanation for Sample 2:
1. The given number is n = 120.
2. Reversing the digits of 120 gives 021, which is treated as 21.
3. Squaring the reversed number: 21 * 21 = 441.
4. The even digits in 441 are 4 and 4.


Expected Time Complexity:
The expected time complexity is O(log n), as the operations depend on the number of digits in 'n'.


Constraints:
1 <= n <= 10^9

Time limit: 1sec
Approaches (1)
Print even from square by reversing the digits
Time Complexity

The expected time complexity is O(log n), as the operations depend on the number of digits in 'n'.

Space Complexity
Code Solution
(100% EXP penalty)
Print even from square by reversing the digits
Full screen
Console