print an integer representing the difference between the sum of the largest digits of each number and sum of second largest digit of each number.
Answers
Answer:
Input: { 4, 6, 2, 7, 9, 8 }
Output: The two numbers with maximum sum are 974 and 862
Input: { 9, 2, 5, 6, 0, 4 }
Output: The two numbers with maximum sum are 952 and 640
We know that a maximum number can be formed from the given digits 0–9 when the largest digit appears first, the second-largest digit appears second, and so on… finally, the smallest digit appears at the end. We can easily extend this logic to solve this problem.
The idea is to sort the given array in descending order and construct two numbers x and y by picking alternate digits from the array, where x is filled with digits at the odd indices, y is filled with digits at the even index of the sorted array.
The two numbers with maximum sum are 974 and 862
Explanation:
4, 6, 2, 7, 9, 8 as input
974 and 862 are the two numbers having the highest total.
9 2, 5, 6, 0, 4 as input
The two numbers with the highest total are 952 and 640.
We know that when the largest digit occurs first, the second-largest digit appears second, and so on, a maximum number may be generated from the provided digits 0–9.
At the very end, the tiniest digit emerges.
This reasoning may simply be extended to tackle this situation.
The goal is to sort the supplied array in ascending order and create two integers x and y by selecting alternative digits from the array, where x is filled with digits at odd indices and y is filled with digits at even indices.