![]() |
Given a number N, the task is to maximize this number by following the given conditions:
Examples:
Naive approach: If a digit in the given number N is even then find the greatest element to its right which is also even and finally swap both. similarly do the same, if the digit is odd. Follow the steps mentioned below to implement the idea:
Below is the implementation of the above approach. C++
Java
Python3
C#
Javascript
Output
8765 Time Complexity: O(N2), Where N is the length of the given string. Efficient approach: This problem can be solved efficiently based on the following idea:
Follow the steps mentioned below to implement the idea.
Below is the implementation of the above approach. C++
Java
Python3
C#
Javascript
Output
8765 Time Complexity: O(M * log M), Where M is the number of digits present in N. |
Reffered: https://www.geeksforgeeks.org
Mathematical |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |