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’.
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.
1 <= T <= 10^4
1 <= A, B, C <= 10^6
Time limit: 1 Sec
2
4554 292 123567
90909 111 876
597
918
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’.
2
324865 123 456
1 22 333
836
123
Can we find a way to extract the digits of a given numbers?
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’:
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’.
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)’.
O(1),
Since we are not using any extra space, space complexity is constant.