2022 Task 2 Max Sum of 2 integers sharing first and last digits

Task 2
There is an array A consisting of N integers.
What’s the maximum sum of two integers from A that share their first and last digits?
For example, 1007 and 167 share their first(1) and last(7) digits, whereas 2002 and 55 do not.

Write a function:

class Solution { public int solution(int[] A); }

that, giving an array A consisting of N integers, returns the maximum sum of two integers that share their first and last digits.
If there are no two integers that share their first and last digits, the function should return -1.

Examples:

  1. Given A =[130, 191, 200, 10], the function should return 140. The only integers in A that share first and last digits are 130 and 10.
  2. Given A =[405, 45, 300, 300], the function should return 600. There are two pairs of integers that share first and last digit: (405, 45) and (300, 300). The sum of the two 300s is bigger than the sum of 405 and 45.
  3. Given A =[50, 222, 49, 52, 25], the function should return -1. There are no two integers that share their first and last digits.
  4. Given A =[30, 909, 3190, 99, 3990, 9009], the function should return 9918.

Write an efficient algorithm for the following assumptions:

  • N is an integer within the range [1 … 100,000];
  • each element of array A is an integer within the range [10 … 1,000,000,000].
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class MaxSum {
    public int solution(int[] A) {
        // 1st iteration : split into several groups of integer set
        //                 each set numbers share the first and last digits
        // 2nd iteration : find top 2 largest integers in each set and add up them afterwards
        //                 update maximum sum result

        Map<String, ArrayList<Integer>> map = new HashMap<>();
        for (int i = 0; i < A.length; i++) {
            String numStr = String.valueOf(A[i]);
            String key = numStr.charAt(0) + "" + numStr.charAt(numStr.length()-1);
            map.computeIfAbsent(key, k -> new ArrayList<>());
            map.get(key).add(A[i]);
        }
        int maxSum = -1;
        for (Map.Entry<String, ArrayList<Integer>> entry : map.entrySet()) {
            if (entry.getValue().size() >= 2) {
                ArrayList<Integer> list = entry.getValue();
                Collections.sort(list, Collections.reverseOrder());
                maxSum = Math.max(maxSum, list.get(0) + list.get(1));
            }
        }
        return maxSum;
    }

    public static void main(String[] args) {
        MaxSum maxSum = new MaxSum();
        System.out.println(maxSum.solution(new int[]{130, 191, 200, 10})); // 140
        System.out.println(maxSum.solution(new int[]{405, 45, 300, 300})); // 600
        System.out.println(maxSum.solution(new int[]{50, 222, 49, 52, 25})); // 0
        System.out.println(maxSum.solution(new int[]{30, 909, 3190, 99, 3990, 9009})); // 9918
    }
}

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值