- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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 −

num1=3224 num2=4321

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

num1=3111 num2=4111

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

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.

#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; }

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

- Related Questions & Answers
- Sorting numbers based on their digit sums in JavaScript
- C Program to Minimum and Maximum prime numbers in an array
- Finding the minimum and maximum value from a string with numbers separated by hyphen in MySQL?
- Digit distance of two numbers - JavaScript
- Possible two sets from first N natural numbers difference of sums as D in C++
- Minimum and Maximum Prime Numbers of a Singly Linked List in C++.
- Maximum absolute difference of value and index sums in C
- Two ways to fetch maximum value from a MySQL column with numbers
- Minimum sum of two numbers formed from digits of an array in C++
- Maximum and Minimum Product Subsets in C++
- Maximum number with same digit factorial product in C++
- Find the minimum distance between two numbers in C++
- 8085 program to find maximum and minimum of 10 numbers
- Largest Even and Odd N-digit numbers in C++
- Program to find maximum sum of two sets where sums are equal in C++

Advertisements