Given a non-negative number num. The problem is to apply at most one swap operation on the number num so that the resultant is the largest possible number. The number could be very large so a string type can be used to store the number.
Examples:
Input : n = 8725634
Output : 8765234
Swapped the digits 2 and 6.
Input : n = 54321
Output : 54321
No swapping of digits required.
Create an array rightMax[]. rightMax[i] contains the index of the greatest digit which is on the right side of num[i] and also greater than num[i]. If no such digit exists then rightMax[i] = -1. Now, traverse the rightMax[] array from i = 0 to n-1(where n is the total number of digits in num), and find the first element having rightMax[i] != -1. Perform the swap(num[i], num[rightMax[i]]) operation and break.
C++
#include <bits/stdc++.h>
using namespace std;
string largestNumber(string num)
{
int n = num.size();
int rightMax[n], right;
rightMax[n - 1] = -1;
right = n - 1;
for (int i = n - 2; i >= 0; i--) {
if (num[i] < num[right])
rightMax[i] = right;
else {
rightMax[i] = -1;
right = i;
}
}
for (int i = 0; i < n; i++) {
if (rightMax[i] != -1) {
swap(num[i], num[rightMax[i]]);
break;
}
}
return num;
}
int main()
{
string num = "8725634";
cout << "Largest number:"
<< largestNumber(num);
return 0;
}
|
Java
public class GFG
{
static String largestNumber(String num)
{
int n = num.length();
int right;
int rightMax[] = new int[n];
rightMax[n - 1] = -1;
right = n - 1;
for (int i = n - 1; i >= 0 ; i--)
{
if (num.charAt(i) < num.charAt(right))
rightMax[i] = right;
else
{
rightMax[i] = -1;
right = i;
}
}
for (int i = 0; i < n; i++)
{
if (rightMax[i] != -1)
{
num = swap(num,i,rightMax[i]);
break;
}
}
return num;
}
static String swap(String num, int i, int j)
{
StringBuilder sb= new StringBuilder(num);
sb.setCharAt(i, num.charAt(j));
sb.setCharAt(j, num.charAt(i));
return sb.toString();
}
public static void main(String[] args)
{
String num = "8725634";
System.out.println("Largest Number : " +
largestNumber(num));
}
}
|
Python3
def largestNumber(num):
n = len(num)
rightMax = [0 for i in range(n)]
rightMax[n - 1] = -1
right = n - 1
i = n - 2
while i >= 0:
if (num[i] < num[right]):
rightMax[i] = right
else:
rightMax[i] = -1
right = i
i -= 1
for i in range(n):
if (rightMax[i] != -1):
t = num[i]
num[i] = num[rightMax[i]]
num[rightMax[i]] = t
break
return num
num = "8725634"
li = [i for i in num]
print("Largest number: ")
li = largestNumber(li)
for i in li:
print(i,end=" ")
print()
|
C#
using System;
using System.Text;
public class GFG
{
static String largestNumber(String num)
{
int n = num.Length;
int right;
int[] rightMax = new int[n];
rightMax[n - 1] = -1;
right = n - 1;
for (int i = n - 1; i >= 0 ; i--)
{
if (num[i] < num[right])
rightMax[i] = right;
else
{
rightMax[i] = -1;
right = i;
}
}
for (int i = 0; i < n; i++)
{
if (rightMax[i] != -1)
{
num = swap(num,i,rightMax[i]);
break;
}
}
return num;
}
static String swap(String num, int i, int j)
{
StringBuilder sb= new StringBuilder(num);
sb[i]=num[j];
sb[j]=num[i];
return sb.ToString();
}
public static void Main()
{
String num = "8725634";
Console.WriteLine("Largest Number : " +largestNumber(num));
}
}
|
PHP
<?php
function largestNumber($num)
{
$n = strlen($num);
$rightMax[$n] = array(0);
$right;
$rightMax[$n - 1] = -1;
$right = $n - 1;
for ($i = $n - 2; $i >= 0; $i--)
{
if ($num[$i] < $num[$right])
$rightMax[$i] = $right;
else
{
$rightMax[$i] = -1;
$right = $i;
}
}
for ($i = 0; $i < $n; $i++)
{
if ($rightMax[$i] != -1)
{
list($num[$i],
$num[$rightMax[$i]]) = array($num[$rightMax[$i]],
$num[$i]);
break;
}
}
return $num;
}
$num = "8725634";
echo "Largest number: ",
largestNumber($num);
?>
|
Javascript
<script>
function largestNumber(num)
{
var n = num.length;
var rightMax = Array(n), right;
rightMax[n - 1] = -1;
right = n - 1;
for (var i = n - 2; i >= 0; i--) {
if (num[i] < num[right])
rightMax[i] = right;
else {
rightMax[i] = -1;
right = i;
}
}
for (var i = 0; i < n; i++) {
if (rightMax[i] != -1) {
var tmp = num[i];
num[i] = num[rightMax[i]];
num[rightMax[i]] = tmp
break;
}
}
return num.join('');
}
var num = "8725634".split('');
document.write( "Largest number:"
+ largestNumber(num));
</script>
|
Output:
Largest number: 8765234
Time Complexity: O(n), where n is the total number of digits.
Auxiliary Space: O(n), where n is the total number of digits.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!