Ctrl K

Pair Sharing a Top Digit

Easy
You are given an array of positive integers values. For each number, look at the largest digit in its decimal form: for 4719 it is 9, for 305 it is 5. Pick two different elements of the array whose largest digits are equal so that their sum is as large as possible. Return that sum, or -1 if no such pair exists.
Example 1
values=[38, 83, 15, 51, 80] → 163

38, 83 and 80 all have 8 as their largest digit; 15 and 51 have 5. The best pair is 83 + 80 = 163.

Example 2
values=[12, 34, 56] → -1

The largest digits 2, 4 and 6 are all different, so no pair qualifies.

Example 3
values=[7, 70, 107, 9] → 177
Constraints
  • 2 ≤ len(values) ≤ 100
  • 1 ≤ values[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair With a Target Sum EasyThe Candidate With Most Votes EasyDoes Any Value Repeat EasyRepeat Within Reach EasyThe Lost Ticket Easy
loading Python…
Input
values=[38, 83, 15, 51, 80]
Expected
163

Run checks the first 3 tests in your browser. Submit checks all 82 tests on the server, hidden ones included.