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].

相关推荐

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

    2024-03-18 23:12:03       17 阅读
  2. 【RL】(task2)策略梯度算法

    2024-03-18 23:12:03       36 阅读
  3. 2024/2/2

    2024-03-18 23:12:03       30 阅读
  4. 作业2024/2/2

    2024-03-18 23:12:03       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-18 23:12:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-18 23:12:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-18 23:12:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-18 23:12:03       20 阅读

热门阅读

  1. 归并排序思路

    2024-03-18 23:12:03       18 阅读
  2. 职场人如何看待领导打绩效

    2024-03-18 23:12:03       16 阅读
  3. Winform编程详解十四:NumericUpDown 数字输入框

    2024-03-18 23:12:03       19 阅读
  4. 使用Cloudflare来给wordpress网站图片自动压缩加速

    2024-03-18 23:12:03       14 阅读
  5. nslookup和dig命令的使用方法以及区别

    2024-03-18 23:12:03       21 阅读
  6. 使用wx:for()

    2024-03-18 23:12:03       21 阅读
  7. 前缀和--k倍区间

    2024-03-18 23:12:03       18 阅读
  8. 中文编程入门(Lua5.4.6中文版)第五章 Lua 函数

    2024-03-18 23:12:03       20 阅读
  9. 从零开始学HCIA之网络自动化01

    2024-03-18 23:12:03       22 阅读
  10. 我手写的轮子开源了

    2024-03-18 23:12:03       20 阅读
  11. http模块 之 如何创建一个http服务?

    2024-03-18 23:12:03       19 阅读