Maximum sum after swapping a digit between two numbers

Updated

Given two natural numbers N1 and N2.The task is to find the maximum possible sum after swapping of a single digit between two given natural numbers.

Examples :

Input :
N1=123 N2=998
Output :
Numbers after 923 and 918
Maximum sum is 1841
Explanation :
1 is swapped with 9 to get the maximized sum
The numbers are 923 and 918
Maximum sum is 1841
Input :
N1=9980 N2=706
Output :
Numbers after 9970 and 806
Maximum sum is 10776

The digits of 123 and 998 in two rows with the 1 and the tens digit 9 swapped, giving the new numbers 923 and 918 with a sum of 1841.

Approach :

  1. Find the Maximum and Minimum of the N1 and N2 in max1 and min1
  2. Find the individual digits of max1 and min1 in array arr1 and arr2 respectively.
  3. if the lengths of two arrays l1 and l2 are unequal, find the index of maximum element in arr2, and then
    iterate arr1 from i equal to l1-1, till i is greater than l2-1, to find the most significant index for swapping.
  4. Else If the above step is not satisfied, iterate both the arrays arr1 and arr2 starting from i is equal to l2-1, and for each corresponding i, find the maximum value from 0 to i – 1 in index1 and index2.
  5. Find the difference between the current significant digit in arrays i.e. arr1[i] and arr2[i] and the digit at
    index arr2[index2] and arr1[index1] and store it in diff1 and diff2
  6. Compare the differences to find the most significant digit and most significant index whose value needs to be swapped.
  7. Finally restore the new numbers from arr1 and arr2 and calculate the maximized sum

Implementation :

// C++ program to find the maximum possible sum by
// swapping of a single digit between two given
// natural numbers

#include<bits/stdc++.h>
using namespace std;
#define MAX 100

// Funcion to find the maximum possible sum
// by swapping of single digit between two 
// given numbers
void findMaxSum(int n1, int n2)
{

    int arr1[MAX] = {0}, arr2[MAX] = {0};
    int l1 = 0, l2 = 0;


    // to find maximum and minimum of
    // N1 and N2
    int max1=max(n1,n2);
    int min1=min(n1,n2);    
    

    // To store every single digit of Maximum number
    // in an array arr1
    for(int i = max1; i > 0; i/=10)
        arr1[l1++] = (i % 10);
    
    // To store every single digit of Minimum number
    // in an array arr2
    for(int i = min1; i > 0; i/=10)
        arr2[l2++] = (i % 10);

    // Comparison to find most significant digit
    // and most significant index for swapping 
    // when length is unequal
    int f = 0;
    if(l1 > l2)
    {
        int index = (max_element(arr2, arr2+l2) - arr2);
        for(int i = l1-1; i > (l2-1); i--)
        {
            if(arr1[i] < arr2[index])
            {
                swap(arr1[i], arr2[index]);
                f = 1;
                break;
            }
        }
    }
    
    if(f != 1)
    {
        int index1 = 0, index2 = 0;
        int diff1 = 0, diff2 = 0;
        for(int i = l2 - 1; i >= 0; i--)
        {
            // To find temporary index of maximum digit present in
            // arr1 and arr2
            index1 = (max_element(arr1, arr1 + i) - arr1);
            index2 = (max_element(arr2, arr2 + i) - arr2);
            
            // to find the difference between the current significant digit 
            // and the digit at temporary index
             diff1 = (arr2[index2] - arr1[i]);
             diff2 = (arr1[index1] - arr2[i]);
    
            // Comparisons to find the most significant position and the
            // most significant digit which need to be swapped
            if( diff1>0 || diff2>0)
            {
                if(diff1 > diff2)
                {
                    swap(arr1[i], arr2[index2]);
                    break;
                }
                else if(diff2 > diff1)
                {
                    swap(arr2[i], arr1[index1]);
                    break;
                }
                else if(diff1 == diff2)
                {
                    if(index1 <= index2)
                    {
                        swap(arr2[i], arr1[index1]);
                        break;
                    }
                    else if(index2 <= index1)
                    {
                        swap(arr1[i], arr2[index2]);
                        break;
                    }
                }
            }
        }
    }
    int f_n1 = 0, f_n2 = 0;
    // To find numbers and the
    // Maximized sum possible
    for(int i = l1 - 1; i >= 0; i--)
    {
        f_n1 = (f_n1 * 10) + arr1[i];
        f_n2= (f_n2 * 10) + arr2[i];
    }

    cout << f_n1 <<" "<< f_n2 << "\n";
    cout << (f_n1 + f_n2) << "\n";
}

// Driver function
int main()
{
    int n1 = 9980;
    int n2 = 706;
    findMaxSum(n1, n2);
    return 0;
}
Output :

9970 806
10776

Time Complexity : O(1)

Comment
Next tutorial
Mithlesh Upadhyay Published 5 Nov 2026 · 4 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials