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