Maximum and minimum sums from two numbers with digit replacements

Given two positive numbers calculate the minimum and maximum possible sums of two numbers. We are allowed to replace digit 5 with digit 6 and vice versa in either or both the given numbers.

Examples :

Input  : x1 = 645 x2 = 666
Output : Minimum Sum: 1100 (545 + 555)
         Maximum Sum: 1312 (646 + 666)

Input: x1 = 5466 x2 = 4555
Output: Minimum sum: 10010
        Maximum Sum: 11132



Since both numbers are positive, we always get maximum sum if replace 5 with 6 in both numbers. And we get minimum sum if we replace 6 with 5 in both numbers. Below is C++ implementation based on this fact.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find maximum and minimum
// possible sums of two numbers that we can
// get if replacing digit from 5 to 6 and vice
// versa are allowed.
#include<bits/stdc++.h>
using namespace std;
  
// Find new value of x after replacing digit
// "from" to "to"
int replaceDig(int x, int from, int to)
{
    int result = 0;
    int multiply = 1;
  
    while (x % 10 > 0)
    {
        int reminder = x % 10;
  
        // Required digit found, replace it
        if (reminder == from)
            result = result + to * multiply;
  
        else
            result = result + reminder * multiply;
  
        multiply *= 10;
        x = x / 10;
    }
    return result;
}
  
// Returns maximum and minimum possible sums of
// x1 and x2 if digit replacements are allowed.
void calculateMinMaxSum(int x1, int x2)
{
    // We always get minimum sum if we replace
    // 6 with 5.
    int minSum = replaceDig(x1, 6, 5) +
                 replaceDig(x2, 6, 5);
  
    // We always get maximum sum if we replace
    // 5 with 6.
    int maxSum = replaceDig(x1, 5, 6) +
                 replaceDig(x2, 5, 6);
    cout << "Minimum sum = " << minSum;
    cout << "nMaximum sum = " << maxSum;
}
  
// Driver code
int main()
{
    int x1 = 5466, x2 = 4555;
    calculateMinMaxSum(x1, x2);
    return 0;

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find maximum and minimum
// possible sums of two numbers that we can
// get if replacing digit from 5 to 6 and vice
// versa are allowed.
class GFG {
      
    // Find new value of x after replacing digit
    // "from" to "to"
    static int replaceDig(int x, int from, int to)
    {
          
        int result = 0;
        int multiply = 1;
      
        while (x % 10 > 0)
        {
            int reminder = x % 10;
      
            // Required digit found, replace it
            if (reminder == from)
                result = result + to * multiply;
      
            else
                result = result + reminder * multiply;
      
            multiply *= 10;
            x = x / 10;
        }
        return result;
    }
      
    // Returns maximum and minimum possible sums of
    // x1 and x2 if digit replacements are allowed.
    static void calculateMinMaxSum(int x1, int x2)
    {
          
        // We always get minimum sum if we replace
        // 6 with 5.
        int minSum = replaceDig(x1, 6, 5) +
                    replaceDig(x2, 6, 5);
      
        // We always get maximum sum if we replace
        // 5 with 6.
        int maxSum = replaceDig(x1, 5, 6) +
                    replaceDig(x2, 5, 6);
        System.out.print("Minimum sum = " + minSum);
        System.out.print("\nMaximum sum = " + maxSum);
    }
      
    // Driver code
    public static void main (String[] args)
    {
        int x1 = 5466, x2 = 4555;
        calculateMinMaxSum(x1, x2);
    }
}
   
// This code is contributed by Anant Agarwal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find maximum 
# and minimum possible sums of 
# two numbers that we can get if
# replacing digit from 5 to 6 
# and vice versa are allowed.
  
  
# Find new value of x after 
# replacing digit "from" to "to"
def replaceDig(x, from1, to):
    result = 0
    multiply = 1
  
    while (x % 10 > 0):
        reminder = x % 10
  
        # Required digit found, 
        # replace it
        if (reminder == from1):
            result = result + to * multiply
  
        else:
            result = result + reminder * multiply
  
        multiply *= 10
        x = int(x / 10)
    return result
  
# Returns maximum and minimum 
# possible sums of x1 and x2 
# if digit replacements are allowed.
def calculateMinMaxSum(x1, x2):
      
    # We always get minimum sum 
    # if we replace 6 with 5.
    minSum = replaceDig(x1, 6, 5) +replaceDig(x2, 6, 5)
  
    # We always get maximum sum
    # if we replace 5 with 6.
    maxSum = replaceDig(x1, 5, 6) +replaceDig(x2, 5, 6)
    print("Minimum sum =" , minSum)
    print("Maximum sum =" , maxSum,end=" ")
  
# Driver code
if __name__=='__main__':
    x1 = 5466 
    x2 = 4555
    calculateMinMaxSum(x1, x2)
  
# This code is contributed 
# by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find maximum and minimum
// possible sums of two numbers that we can
// get if replacing digit from 5 to 6 and vice
// versa are allowed.
using System;
  
class GFG {
      
    // Find new value of x after 
    // replacing digit "from" to "to"
    static int replaceDig(int x, int from
                                 int to)
    {
        int result = 0;
        int multiply = 1;
      
        while (x % 10 > 0)
        {
            int reminder = x % 10;
      
            // Required digit found,
            // replace it
            if (reminder == from)
                result = result + to * multiply;
      
            else
                result = result + reminder * multiply;
      
            multiply *= 10;
            x = x / 10;
        }
        return result;
    }
      
    // Returns maximum and minimum 
    // possible sums of x1 and x2
    // if digit replacements are allowed.
    static void calculateMinMaxSum(int x1, int x2)
    {
          
        // We always get minimum sum if 
        // we replace 6 with 5.
        int minSum = replaceDig(x1, 6, 5) +
                     replaceDig(x2, 6, 5);
      
        // We always get maximum sum if
        //  we replace 5 with 6.
        int maxSum = replaceDig(x1, 5, 6) +
                       replaceDig(x2, 5, 6);
        Console.Write("Minimum sum = " + minSum);
        Console.Write("\nMaximum sum = " + maxSum);
    }
      
    // Driver code
    public static void Main ()
    {
        int x1 = 5466, x2 = 4555;
        calculateMinMaxSum(x1, x2);
    }
}
  
// This code is contributed by Nitin Mittal.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find maximum 
// and minimum possible sums of 
// two numbers that we can get if
// replacing digit from 5 to 6 
// and vice versa are allowed.
  
  
// Find new value of x after 
// replacing digit "from" to "to"
function replaceDig($x, $from, $to)
{
    $result = 0;
    $multiply = 1;
  
    while ($x % 10 > 0)
    {
        $reminder = $x % 10;
  
        // Required digit found, 
        // replace it
        if ($reminder == $from)
            $result = $result + $to
                           $multiply;
  
        else
            $result = $result
                      $reminder
                      $multiply;
  
        $multiply *= 10;
        $x = $x / 10;
    }
    return $result;
}
  
// Returns maximum and minimum 
// possible sums of x1 and x2 
// if digit replacements are allowed.
function calculateMinMaxSum($x1, $x2)
{
    // We always get minimum sum 
    // if we replace 6 with 5.
$minSum = replaceDig($x1, 6, 5) +
           replaceDig($x2, 6, 5);
  
    // We always get maximum sum
    // if we replace 5 with 6.
    $maxSum = replaceDig($x1, 5, 6) +
               replaceDig($x2, 5, 6);
    echo "Minimum sum = " , $minSum,"\n";
    echo "Maximum sum = " , $maxSum;
}
  
// Driver code
$x1 = 5466; $x2 = 4555;
calculateMinMaxSum($x1, $x2);
  
// This code is contributed 
// by nitin mittal. 
?>

chevron_right



Output :

Minimum sum = 10010
Maximum sum = 11132

This article is contributed by Roshni Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.



My Personal Notes arrow_drop_up

Improved By : nitin mittal, Mithun Kumar