Maximum number formed from the digits of given three numbers

07 Jul, 2021
Given 3 four-digit integers A, B, and C, the task is to print the number formed by taking the maximum digit from all the digits at the same positions in the given numbers.
Examples:
Input: A = 3521, B = 2452, C = 1352Output: 3552Explanation:
The maximum of the digits that are at 1th place is equal to max(A[3] = 1, B[3] = 2, C[3] = 2) 2.The maximum of the digits that are at 10th place is equal to max(A[2] = 2, B[2] = 5, C[2] = 5) 5.The maximum of the digits that are at 100th place is equal to max(A[1] = 5, B[1] = 4, C[1] = 3) 5.The maximum of the digits that are at 1000th place is equal to max(A[0] = 3, B[0] = 3, C[0] = 1) 3.
The maximum of the digits that are at 1th place is equal to max(A[3] = 1, B[3] = 2, C[3] = 2) 2.
The maximum of the digits that are at 10th place is equal to max(A[2] = 2, B[2] = 5, C[2] = 5) 5.
The maximum of the digits that are at 100th place is equal to max(A[1] = 5, B[1] = 4, C[1] = 3) 5.
The maximum of the digits that are at 1000th place is equal to max(A[0] = 3, B[0] = 3, C[0] = 1) 3.
Therefore, the number formed is 3552.
Input: A = 11, B = 12, C = 22Output: 22
Approach: The problem can be solved by iterating over the digits of the given integers. Follow the steps below to solve the problem:
Initialize a variable, say ans as 0 and P as 1 to store the maximum number possible and the position value of a digit.
Iterate until A, B and C are greater than 0 and perform the following steps:Find the digits at the unit places of the numbers A, B, and C and store them in variables say a, b and c respectively.Update A to A/10, B to B/10, and C to C/10.Increment ans by the P*max(a, b, c) and then update P to P*10.
Find the digits at the unit places of the numbers A, B, and C and store them in variables say a, b and c respectively.
Update A to A/10, B to B/10, and C to C/10.
Increment ans by the P*max(a, b, c) and then update P to P*10.
Finally, after completing the above steps, print the answer stored in ans.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to find the maximum number// formed by taking the maximum digit// at the same position from each numberint findkey(int A, int B, int C){    // Stores the result    int ans = 0;     // Stores the position value of a    // digit    int cur = 1;     while (A > 0) {         // Stores the digit at the unit        // place        int a = A % 10;        // Stores the digit at the unit        // place        int b = B % 10;        // Stores the digit at the unit        // place        int c = C % 10;         // Update A, B and C        A = A / 10;        B = B / 10;        C = C / 10;         // Stores the maximum digit        int m = max(a, max(c, b));         // Increment ans cur*a        ans += cur * m;         // Update cur        cur = cur * 10;    }    // Return ans    return ans;} // Driver Codeint main(){    // Given Input    int A = 3521, B = 2452, C = 1352;     // Function call    cout << findkey(A, B, C);    return 0;}
// Java program for the above approachpublic class GFG{ // Function to find the maximum number// formed by taking the maximum digit// at the same position from each numberstatic int findkey(int A, int B, int C){       // Stores the result    int ans = 0;     // Stores the position value of a    // digit    int cur = 1;     while (A > 0) {         // Stores the digit at the unit        // place        int a = A % 10;               // Stores the digit at the unit        // place        int b = B % 10;               // Stores the digit at the unit        // place        int c = C % 10;         // Update A, B and C        A = A / 10;        B = B / 10;        C = C / 10;         // Stores the maximum digit        int m = Math.max(a, Math.max(c, b));         // Increment ans cur*a        ans += cur * m;         // Update cur        cur = cur * 10;    }    // Return ans    return ans;} // Driver Codepublic static void main(String args[]){    // Given Input    int A = 3521, B = 2452, C = 1352;     // Function call    System.out.println(findkey(A, B, C));    }} // This code is contributed by SoumikMondal
# Py program for the above approach # Function to find the maximum number# formed by taking the maximum digit# at the same position from each numberdef findkey(A, B, C):    # Stores the result    ans = 0     # Stores the position value of a    # digit    cur = 1     while (A > 0):         # Stores the digit at the unit        # place        a = A % 10        # Stores the digit at the unit        # place        b = B % 10        # Stores the digit at the unit        # place        c = C % 10         # Update A, B and C        A = A // 10        B = B // 10        C = C // 10         # Stores the maximum digit        m = max(a, max(c, b))         # Increment ans cur*a        ans += cur * m         # Update cur        cur = cur * 10     # Return ans    return ans # Driver Codeif __name__ == '__main__':    # Given Input    A = 3521    B = 2452    C = 1352     # Function call    print (findkey(A, B, C))     # This code is contributed by mohit kumar 29.
// C# program for the above approachusing System; class GFG{     // Function to find the maximum number// formed by taking the maximum digit// at the same position from each numberstatic int findkey(int A, int B, int C){       // Stores the result    int ans = 0;     // Stores the position value of a    // digit    int cur = 1;     while (A > 0) {         // Stores the digit at the unit        // place        int a = A % 10;               // Stores the digit at the unit        // place        int b = B % 10;               // Stores the digit at the unit        // place        int c = C % 10;         // Update A, B and C        A = A / 10;        B = B / 10;        C = C / 10;         // Stores the maximum digit        int m = Math.Max(a, Math.Max(c, b));         // Increment ans cur*a        ans += cur * m;         // Update cur        cur = cur * 10;    }    // Return ans    return ans;} // Driver Codestatic public void Main (){         // Given Input    int A = 3521, B = 2452, C = 1352;     // Function call    Console.Write(findkey(A, B, C));}} // This code is contributed by sanjoy_62.
<script>// JavaScript program for the above approach // Function to find the maximum number// formed by taking the maximum digit// at the same position from each numberfunction findkey(A,  B, C){     // Stores the result    let ans = 0;     // Stores the position value of a    // digit    let cur = 1;     while (A > 0)    {         // Stores the digit at the unit        // place        let a = A % 10;                 // Stores the digit at the unit        // place        let b = B % 10;                 // Stores the digit at the unit        // place        let c = C % 10;         // Update A, B and C        A = Math.floor(A / 10);        B = Math.floor(B / 10);        C = Math.floor(C / 10);         // Stores the maximum digit        let m = Math.max(a, Math.max(c, b));         // Increment ans cur*a        ans += cur * m;         // Update cur        cur = cur * 10;    }         // Return ans    return ans;} // Driver Code// Given Input    let A = 3521, B = 2452, C = 1352;     // Function call     document.write(findkey(A, B, C));     // This code is contributed by Potta Lokesh     </script>
  
3552
 
Time Complexity: O(log(N))Auxiliary Space: O(1)
 
mohit kumar 29
SoumikMondal
sanjoy_62
lokeshpotta20
number-digits
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Merge two sorted arrays with O(1) extra space
Program to print prime numbers from 1 to N.
Find next greater number with same set of digits
Segment Tree | Set 1 (Sum of given range)
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java