


You are given an array ‘ARR’ of length ‘N’. There are two operations, ‘FIRST_SUM’ and ‘LAST_SUM’ for every index ‘i’ (1 <= i <= N) in the array,
i) FIRST_SUM(i) calculates the sum of first i numbers.
ii) LAST_SUM(i) calculates the sum of last N-i+1 numbers.
Also for every ‘i’, SPECIAL_SUM(i) can be calculated as:
SPECIAL_SUM(i) = FIRST_SUM(i) + LAST_SUM(i)
Your task is to return the minimum SPECIAL_SUM for 0 <= i <= N - 1.
For example:
Given ‘N’ = 4 and ‘ARR’ = [1, 2, 3, 4].
Then the minimum special sum will be 5 for i = 0 (0-based indexing), which is (1 + 4) = 5.Sum of 1 integer from beginning and end.
For i = 1 it will be (1 + 2) + (3 + 4) = 10
For i = 2 it will be (1 + 2 + 3) + (2 + 3 + 4) = 15
For i = 3 it will be (1 + 2 + 3 + 4) + (1 + 2 + 3 + 4) = 20
All of which are greater than 5.
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains a single integer N, where ‘N’ is the number of elements of the array.
The second line of each test case contains ‘N’ space-separated integers, denoting the array elements.
Output format:
For each test case, return the minimum SPECIAL_SUM for ‘i’ in the range [ 0, N-1 ].
The output of each test case will be printed in a separate line.
Note:
You don’t need to print anything. You just need to implement the given function.
Constraints:
1 <= T <= 5
1 <= N <= 5 *10^3
-5 *10^2 <= ARR[i] < 5 *10^2
Time limit: 1 sec
2
4
1 2 3 4
4
1 -2 -3 4
5
-5
For the first test case:
The minimum special sum will be 5 for i = 0 (0-based indexing), which is (1 + 4) = 5.
For i = 1 it will be (1 + 2) + (3 + 4) = 10
For i = 2 it will be (1 + 2 + 3) + (2 + 3 + 4) = 15
For i = 3 it will be (1 + 2 + 3 + 4) + (1 + 2 + 3 + 4) = 20
All of which are greater than 5.
For the second test case:
The minimum special sum will be -5 for i = 2 (0-based indexing), which is (1 + (-2) + (-3)) + (-2 + (-3) + (4)) = -5.
For i = 0 it will be (1) + (4) = 5
For i = 1 it will be (1 + (-2) ) + ( (-3) + 4) = 0
For i = 3 it will be (1 + (-2) + (-3) + 4) + (1 + (-2) + (-3) + 4) = 0.
All of which are less than -5.
2
5
1 2 -5 3 1
5
1 1 1 1 1
-3
2
Do we need to calculate firstSum and lastSum for every i?
The main idea is to calculate the ‘FIRST_SUM’ and the ‘LAST_SUM’ for every index ‘i’ between [0, N - 1].
O(N ^ 2), where ‘N’ is the length of the given array.
We have to traverse the array N times. Therefore, the net time complexity will be O(N ^ 2).
O(1).
Since we are not using any extra space to keep track of the elements.