Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

return the maximum sum of two numbers whose digits add up to an equal sum

    public static void findMaximum(Integer[] input)
    {
        // base case
        if (input.length <= 1) {
            return;
        }
 
        // sort the array in descending order
        Arrays.sort(input, Comparator.reverseOrder());
 
        // fill `x` with digits at the odd indices of the sorted array
        int x = 0;
        for (int i = 0; i < input.length; i = i + 2) {
            x = x * 10 + input[i];
        }
 
        // fill `y` with digits at the even indices of the sorted array
        int y = 0;
        for (int i = 1; i < input.length; i = i + 2) {
            y = y * 10 + input[i];
        }
 
        // print `x` and `y`
        System.out.println("The two numbers with maximum sum are "
                + x + " and " + y);
    }
Source by www.techiedelight.com #
 
PREVIOUS NEXT
Tagged: #return #maximum #sum #numbers #digits #add #equal #sum
ADD COMMENT
Topic
Name
2+9 =