
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
#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
- Related Articles
- Sorting numbers based on their digit sums in JavaScript
- C++ program to find minimum difference between the sums of two subsets from first n natural numbers
- Finding the minimum and maximum value from a string with numbers separated by hyphen in MySQL?
- Two ways to fetch maximum value from a MySQL column with numbers
- What is the minimum and maximum number of digits in the sum if we add any two 3 digit number
- C Program to Minimum and Maximum prime numbers in an array
- Possible two sets from first N natural numbers difference of sums as D in C++
- 8085 program to find maximum and minimum of 10 numbers
- Digit distance of two numbers - JavaScript
- Select the minimum value from the maximum values of two tables with a single MySQL\nquery?
- Minimum and Maximum Prime Numbers of a Singly Linked List in C++.
- Path With Maximum Minimum Value in Python
- Maximum absolute difference of value and index sums in C
- Minimum sum of two numbers formed from digits of an array in C++
- How to read Maximum and Minimum temperature in Six's Maximum and Minimum Thermometer?
