Maximum and minimum sums from two numbers with digit replacements in C++


We are given with two positive numbers num1 and num2. The goal is to find the minimum sum and maximum sum possible of these two after a digit replacement in both of them. We are allowed to replace digits from each of numbers in both of numbers. Suppose num1 is 434 and num2 is 324 and we can replace digit 3 with 4 and digic 4 with 3. Then minimum sum will be − 333+323=656 and maximum sum will be 444+424=864.

Let us understand with examples for digit replacement 3 with 4 and vice versa −

Input

num1=3224 num2=4321

Output

Maximum sum is : 8645
Minimum sum is : 6544

Explanation − replacing all 3s with 4s to make both numbers larger as 4 is greater than 3.

num1 becomes 4224 and num2 becomes 4421 and sum is 8645 replacing all 4s with 3s to make both numbers smaller as 3 is lesser than 4.

num1 becomes 3223 and num2 becomes 3321 and sum is 6544

Input

num1=3111 num2=4111

Output

Maximum sum is : 8222
Minimum sum is : 6222

Explanation − replacing all 3s with 4s to make both numbers larger as 4 is greater than 3.

num1 becomes 4111 and num2 becomes 4111 and sum is 8222 replacing all 4s with 3s to make both numbers smaller as 3 is lesser than 4.

num1 becomes 3111 and num2 becomes 3111 and sum is 6222

Approach used in the below program is as follows

  • The numbers are present in variables num1 and num2.

  • Function calculateSum ( int n1,int n2) is used to calculate the minimum and maximum sum of numbers after digit replacement.

  • It takes two numbers n1 and n2 as parameters and displays the result stored in minSum and maxSum.

  • At first we replace every 4 in both numbers by 3 and store new values in num2 and num2 by calling replace(n1,4,3) and replace(n2,4,3) for both respectively.

  • Calculate minimum sum by adding new num1 and num2.

  • Similarly repeat above steps by calling replace(n1,3,4) and replace(n2,3,4) for replacing every 3 by 4 and calculate the maximum sum.

  • The function replace(int x,int digit1,int digit2) replaces every digit1 in x by digit2 and returns the new number.

  • Variable number stores the newly obtained number, initialized with 0.

  • temp is used to store the multiplier by 10 for each iteration.

  • We will take each digit from the right hand by dividing x by 10 and storing the remainder in remainder in rem.

  • If rem is equal to digit1 replace it with digit2. Add this to obtain new number = number + digit2 * temp;

  • Otherwise no change number=number + rem*temp;

  • Reduce x by dividing it by 10 and increase the multiplier by 10. (remp=temp*10)

  • Return the number obtained.

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
//replace digit1 with digit2
int replace(int x, int digit1, int digit2){
   int number = 0;
   int temp = 1;
   while (x > 0){
      int rem = x % 10;
      // Required digit found, replace it
      if (rem == digit1)
         number = number + digit2 * temp;
      else
         number = number + rem * temp;
         temp *= 10;
         x = x / 10;
      }
      return number;
   }
   void calculateSum(int n1, int n2){
      //replace 4 by 3
      int num1=replace(n1,4,3);
      int num2=replace(n2,4,3);
      int minSum=num1+num2;
      //replace 3 by 4
      num1=replace(n1,3,4);
      num2=replace(n2,3,4);
      int maxSum=num1+num2;
      std::cout << "Minimum Sum by digit replacement: " << minSum;
      std::cout << "\nMaximum Sum by digit replacement: " << maxSum;
}
int main(){
   int num1 = 3131, num2 = 4141;
   calculateSum(num1, num2);
   return 0;
}

Output

Minimum Sum by digit replacement: 6262
Maximum Sum by digit replacement: 8282

Updated on: 28-Jul-2020

237 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements