
- 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
Maximize the value of A by replacing some of its digits with digits of B in C++
The task is to maximize the value of number A by replacing some of its digits with digits present in another number B. No digits will be replaced if A’s value cannot be maximized.
Note − a digit from B can be used only once.
Let’s now understand what we have to do using an example −
Input
A = “1221” B = “1211”
Output
Maximum value of A possible 2221
Explanation − We here select 2 from B and replace it with the first 1 of A. Here it is the only choice as replacing any other digit of A with either 2 or 1 will not increase it’s value.
Input
A = “1002” B = “3200”
Output
Maximum value of A possible 3202
Approach used in the below program as follows
Each of the digits in A will be replaced with a digit of B if it is less than that of B.
Sort the string B in ascending order.
Start traversing A from left.
Now traverse B from right.
Replace the digit in A with that of B if it is smaller and increment pointer at A and decrement pointer at B.
Example
#include <bits/stdc++.h> using namespace std; // Function to return the maximized value of a string valueup(string str1, string str2){ // Sort digits in ascending order sort(str2.begin(), str2.end()); int len1 = str1.length(); int len2 = str2.length(); int j = len2 - 1; for (int i = 0; i < len1; i++) { // If all the digits of b consumed if (j < 0) break; if (str2[j] > str1[i]) { str1[i] = str2[j]; j--; //once digit used } } return str1; } // Driver code int main(){ string a = "1204"; string b = "4521"; cout << valueup(a, b); return 0; }
Output
If we run the above code we will get the following output −
5424
- Related Articles
- Maximize the given number by replacing a segment of digits with the alternate digits given in C++
- A two digit number is 4 times the sum of its digits and twice the product of its digits. Find the number.
- Program to find latest valid time by replacing hidden digits in Python
- Number of Digits in a^b in C++
- Replacing digits to form binary using JavaScript
- Find the Largest number with given number of digits and sum of digits in C++
- The sum of a two digit number and the number obtained by reversing the order of its digits is 99. If the digits differ by 3, find the number.
- Find smallest number with given number of digits and sum of digits in C++
- A two-digit number is 4 times the sum of its digits and twice the product of the digits. Find the number.
- Find numbers whose sum of digits equals a value
- C Program to check if a number is divisible by sum of its digits
- C Program to check if a number is divisible by any of its digits
- Number of digits in the nth number made of given four digits in C++
- JavaScript Program to Find Maximum value possible by rotating digits of a given number
- How to split last n digits of each value in the array with JavaScript?
