Facebook Pixel

Google Online Assessment 2022 (OA) - Maximum Reversed Number

One fragment of a given integer N can be selected and its digits reversed (replaced with a right to left version of themselves). What is the maximum number that can be obtained this way from integer N?

Input

  • n: an integer 1<=N<=10^9

Output

the greatest integer that can be created by reversing a subset(substring) of its digits

Examples

Example 1:

Input:

n = 5340

Output: 5430

Explanation:

Given N = 5340, the answer is 5430. Fragment "34" can be reversed to "43".

Example 2:

Input:

n = 2043

Output: 4023

Explanation:

Given N = 2043, the answer is 4023. Fragment "204" can be reversed to "402".

Example 3:

Input:

n = 620

Output: 620

Explanation:

Given N = 620, the answer is 620. There is no need to reverse any fragment.

Constraints

  • 1<=N<=10^9

Try it yourself

Solution

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro