Sum of Largest Neighbors
Given an array of non-negative integers, compute the sum of the two largest unique numbers that are direct neighbors in the array. If the array does not have two unique neighbors with different values, return 0.
[ 7, 10, 2, 10, 3 ]
Explanation. The two largest neighbors are 7 and 10 at indexes 0 and 1, respectively. Their sum is 17.
[ 23, 23, 23 ]
Explanation. All elements are same, hence there are no two largest unique neighbors.
[ 5 ]
Explanation. Single element arrays cannot have neighbors.
[ 1, 2 ]
Explanation. The only neighbors are 1 and 2, and they are the largest unique numbers. Their sum is 3.
Follow-up: Consider extending the solution to handle arrays with negative integers and ensuring performance remains optimal even with the maximum constraints.
The input array should contain at least one but no more than 1000 elements. Each element should be a non-negative integer.
- Views
- 3