Concatenate The Largest Digit

Easy
0/40
Average time to solve is 15m
profile
Contributed by
5 upvotes
Asked in companies
SalesforceOlaCGI

Problem statement

You are given three non-zero numbers ‘A’, ‘B’, and ‘C’. The task is to find the number formed by concatenating the largest digit from each of these numbers in the order of ‘A’, ‘B’, and ‘C’.

For Example :
A = 5678, B = 45 and C = 769
The largest digit in ‘A’ is ‘8’, ‘B’ is ‘5’, and ‘C’ is ‘9’. The new number formed by concatenating the largest digit from each of these numbers is ‘859’. So, the answer is ‘859’.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line of input contains an integer ‘T’ which denotes the number of test cases. Then, the ‘T’ test cases follow.

The first and only line of each test case contains three space-separated numbers, ‘A’, ‘B’, and ‘C’, denoting the given numbers.
Output Format :
For every test case, return the number formed by concatenating the largest digit from ‘A’, ‘B’, and ‘C’.
Note :
You do not need to print anything; it has already been taken care of. Just implement the function.
Constraints :
1 <= T <= 10^4
1 <= A, B, C <= 10^6

Time limit: 1 Sec

Sample Input 1 :

2
4554 292 123567
90909 111 876

Sample Output 1 :

597
918

Explanation Of Sample Input 1 :

Test Case 1:
a = 4554, b = 292 and c = 123567
The largest digit in ‘a’ is ‘5’, ‘b’ is ‘9’, and ‘c’ is ‘7’. The new number formed by concatenating the largest digit from each of these numbers is ‘597’. So, the answer is ‘597’. 

Test Case 2:
a = 90909, b = 111 and c = 876
The largest digit in ‘a’ is ‘9’, ‘b’ is ‘1’, and ‘c’ is ‘8’. The new number formed by concatenating the largest digit from each of these numbers is ‘918’. So, the answer is ‘918’.

Sample Input 2 :

2
324865 123 456
1 22 333

Sample Output 2 :

836
123
Hint

Can we find a way to extract the digits of a given numbers?

Approaches (1)
Find All Digits

A simple and efficient approach is to find all the digits of the given number and find the largest digit from them. For a number ‘x’, the digit at units place is equal to ‘x%10’ (‘%’ gives the division’s remainder). If we divide ‘x’ by ‘10’, each digit of ‘x’ will be shifted to the right, and the digit at the hundreds place will be shifted to units place. Now ‘x%10’ will give the digit at the hundreds place in the original ‘x’. So, keep dividing ‘x’ by ‘10’ until ‘x’ becomes ‘0’ to find all the digits of ‘x’. 

The ‘largestDigit(integer x)’ function. Used to find the largest digit in ‘x’:

  • Initialize integer ‘res = 0’. Use it to find the largest digit in ‘x’.
  • Run a loop until ‘x’ is not equal to ‘0’:
    • If ‘x%10’ is greater than ‘res’, then:
      • ‘res = x’.
    • ‘x = x/10’. To shift all digits of ‘x’ to the right.
  • Return ‘res’.

Similarly, to concatenate a digit to ‘x’, multiply ‘x’ by ‘10’ and add the digit to the new value of ‘x’. Use ‘largestDigit’ to find the largest digit in ‘A’, ‘B’, ‘C’ and concatenate it to ‘res’.

  • Initialize integer ‘res’. Use it to store the answer.
  • ‘res = largestDigit(A)’. Store the largest digit of ‘A’ in ‘res’.
  • ‘res = (res*10) + largestDigit(B)’. Concatenate the largest digit in ‘B’ to ‘res’.
  • ‘res = (res*10) + largestDigit(C)’. Concatenate the largest digit in ‘C’ to ‘res’.
  • Return ‘res’ as the answer.
Time Complexity

O(log(A) + log(B) + log(C)), where ‘A’, ‘B’ and ‘C’ are the given numbers.

 

A count of digits in the number ‘x’ is equal to ‘log(x)’ (to the base 10). So, the time required to find each digit of ‘A’, ‘B’ and ‘C’ is equal to ‘Log(A) + Log(B) + Log(C)’.

Space Complexity

O(1),

 

Since we are not using any extra space, space complexity is constant.

Code Solution
(100% EXP penalty)
Concatenate The Largest Digit
Full screen
Console