LargeSmallSum
Accenture coding challenges question, verified with a worked answer. Free to practise - no sign-up.
Implement the function LargeSmallSum(arr). The function accepts an integer array arr. Return the sum of the second largest element from the even positions and the second smallest element from the odd positions. Assume 0th position is even. Return 0 if the array is empty or length is 3 or less.
Input format
The first line contains the size of the array. The second line contains the elements of the array.
Output format
A single integer as per the rules.
Constraints
All array elements are unique.
Sample tests
Sample 1
Input
6
3 2 1 7 5 4
Expected
7Sample 2
Input
7
1 8 0 2 3 5 6
Expected
8Show a reference solution
Reference solution
def LargeSmallSum(arr):
if len(arr) <= 3:
return 0
evens = sorted(arr[0::2])
odds = sorted(arr[1::2])
if len(evens) < 2 or len(odds) < 2:
return 0
return evens[-2] + odds[1]