Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

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

// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find sum of digits
static int digitSum(long n)
{
    int sum = 0;
    while (n > 0)
    {
        sum += (n % 10);
        n /= 10;
    }
    return sum;
}
 
// Function to find maximum sum pair
// having the same sum of digits
static void findMax(int []arr, int n)
{
   
    // Map to store the sum of digits
    // in a number as the key and
    // the maximum number having
    // that sum of digits as the value
    HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
    int ans = -1, pairi = 0, pairj = 0;
    for (int i = 0; i < n; i++) {
 
        // Store the current sum of digits
        // of the number in temp
        int temp = digitSum(arr[i]);
 
        // If temp is already present
        // in the map then update
        // ans if the sum is greater
        // than the existing value
        if (mp.containsKey(temp)) {
            if (arr[i] + mp.get(temp) > ans) {
                pairi = arr[i];
                pairj = mp.get(temp);
                ans = pairi + pairj;
            }
            mp.put(temp, Math.max(arr[i], mp.get(temp)));
        }
        else
        // Change the value in the map
        mp.put(temp, arr[i]);
         
    }
 
    System.out.print(pairi+ " " +  pairj
        + " " +  ans +"
");
}
 
// Driver Code Starts.
public static void main(String[] args)
{
    int []arr = { 55, 23, 32, 46, 88 };
    int n = arr.length;
    findMax(arr, n);
}
}
 
// This code is contributed by shikhasingrajput
Comment

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);
    }
Comment

PREVIOUS NEXT
Code Example
Java :: java install in fedora 
Java :: core java tutorial 
Java :: how to use deque as stack or queue in java? 
Java :: junit vintage engine maven 
Java :: final method meaning 
Java :: string.join java 8 
Java :: set size button java 
Java :: Index through 2d array 
Java :: android xml hide 
Java :: 8.1.1. Declaring an Array&para; 
Java :: java catch multiple exceptions 
Java :: fileinputstream 
Java :: str.substring last 2 java 
Java :: Printing array list through for loop in java 
Java :: get frequency of letters java 
Java :: Duration class java 
Java :: tic tac toe in java 
Java :: convert javascritp getTime() to datetime 
Java :: How to find the logged-in user in Spring Boot? 
Java :: react The href attribute is required for an anchor to be keyboard 
Java :: springboot mongodb test 
Java :: native insert query in jpa repository 
Java :: flutter java.lang.RuntimeException: Unable to instantiate activity ComponentInfo 
Java :: java run code at interval 
Java :: Cause: zip END header not found 
Java :: arraylist input from user java 
Java :: dot operator java 
Java :: java convert double to int 
Java :: java is list ordered 
Java :: restart java windows 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =