An observatory technician is calibrating a rack of telescope lenses before bolting two of them together into a dual-lens rig. Each lens carries a magnification rating. Mounting any two distinct lenses together introduces a fixed bracket overhead that reduces each mounted lens's effective magnification by exactly 1 unit. The technician wants to choose two distinct lenses from the rack so that the product of their two overhead-adjusted magnifications is as large as possible.
Given the magnification ratings of every lens on the rack, determine the maximum possible value of (a - 1) * (b - 1) taken over every pair of distinct lenses with ratings a and b.
n, the number of lenses.n space-separated integers, the magnification rating of each lens.A single integer: the maximum achievable value of (a - 1) * (b - 1) over all pairs of distinct lenses.
Example 1
Input
3 3 4 5
Expected
12
Explanation
Pairing the lenses rated 4 and 5 gives (4-1)*(5-1) = 3*4 = 12. Pairing 3 with 4 gives 2*3=6 and pairing 3 with 5 gives 2*4=8, both smaller, so 12 is the maximum.
Example 2
Input
4 1 5 4 5
Expected
16
Explanation
The two lenses rated 5 give (5-1)*(5-1) = 4*4 = 16. Pairing a 5 with the 4 gives 4*3=12, and pairing with the 1 gives 0, both smaller, so 16 is the maximum.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →