Given two natural numbers N1 and N2, the task is to find the maximum sum possible after swapping of a single digit between them.
Input: N1 = 984788, N2 = 706
Swapping 4 from N1 with 7 from N2, we get N1 = 987788 and N2 = 406
Sum = 988194
Input: N1 = 9987, N2 = 123
Swapping 8 from N1 with 1 from N2, we get N1 = 9917 and N2 = 823
Sum = 10740
- Compare N1 and N2 and store the digits of the larger of the two in array arr1 and that of the smaller in arr2 respectively.
- If both the numbers are of different lengths, find the index of maximum element in arr2, and the most significant index in arr1, and swap them to maximize the sum.
- If both the numbers are of same length,
- Iterate both the arrays arr1 and arr2 at the same time.
- For each digit at index i in both the arrays, find the difference between the current digit and the largest digit left to index ‘i’.
- Compare the difference to find the most significant digit and most significant index, whose value needs to be swapped.
- Restore the new numbers from arr1 and arr2 and calculate the maximized sum.
Below code is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Form the largest number using at most one swap operation
- Form the smallest number using at most one swap operation
- GCD of two numbers when one of them can be very large
- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
- Maximise array sum after taking non-overlapping sub-arrays of length K
- Maximise the number of toys that can be purchased with amount K using min Heap
- Find two numbers with sum N such that neither of them contains digit K
- Maximise the size of consecutive element subsets in an array
- Maximise the number of toys that can be purchased with amount K
- Count of arrays in which all adjacent elements are such that one of them divide the another
- Minimum number of integers required such that each Segment contains at least one of them
- Check whether it is possible to join two points given on circle such that distance between them is k
- Maximum subset sum such that no two elements in set have same digit in them
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- How to swap two numbers without using a temporary variable?
- Next higher number using atmost one swap operation
- Largest smaller number possible using only one swap operation
- C Program to Swap two Numbers
- Largest number with one swap allowed
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.