Skip to main content

JP Morgan

JP Morgan OA 2022 - Multiply the Subarray Sums


Implement the following function:

int ProductOfSums(int arr[], int n);

The function accepts a positive integer array arr of length n as its argument. The array can be divided into two parts:

  1. The first part of the array (starting from index 0 up to i) is in ascending order.
  2. The second part of the array (starting from index i up to n-1) is in descending order.

Task:
Implement the function to find:

  • The sum of the first part (ascending order).
  • The sum of the second part (descending order).

Return:
The product of the sum of the first and second parts.

Assumption:

  • All elements in the array are unique.

Note:

  • Return -1 if n < 3 or the array is null (or None in case of Python).
  • The computed value lies within the integer range.

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.

Custom Input Format for the Above Case:

7
3 8 14 12 10 7 4

(The first line represents the size of the array n and the second line represents the elements of the array arr.)

Sample Input:

arr: [5, 6, 7, 8, 4, 3]

Sample Output:

390

Custom Input Format for the Above Case:

6
5 6 7 8 4 3

(The first line represents the size of the array n and the second line represents the elements of the array arr.)