JP Morgan OA-1 2022
Problem Description
You need to write the following function:
int ProductOfSums(int arr[], int n)
;
The function takes an integer array arr
of size n
as input, where:
- The first portion of the array (starting from index 0 up to some index
i
) is sorted in ascending order. - The second portion of the array (starting from index
i
to the end) is sorted in descending order.
Objective:
- Calculate the sum of the first portion (in ascending order).
- Calculate the sum of the second portion (in descending order).
- Return the product of both sums.
Conditions:
- If
n
is less than 3, or if the array isnull
(orNone
in Python), the function should return-1
. - Assume all elements in the array are distinct.
- The final result will be within the limits of an integer.
Example:
Input:
arr: [3, 8, 14, 12, 10, 7, 4]
Output:
1175
Explanation:
- Sum of first part (ascending order):
(3 + 8 + 14 = 25) - Sum of second part (descending order):
(14 + 12 + 10 + 7 + 4 = 47) - Product of sums:
(25 times 47 = 1175)
Thus, the output is 1175.
Input:
arr: [5, 6, 7, 8, 4, 3]
Output:
390