
If the given array is [6,7,1,8,5]. We can split the array into [6,7,1] and [8,5].
Hence the answer will be |14-13| = 1 which is minimum.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains a single integer, 'N,’ denoting the number of elements in 'ARR'.
The second line of each test case has ‘N’ integers corresponding to the elements of 'ARR'.
Output Format:
For each test case, print a single integer corresponding to the minimum absolute difference possible.
Print the output of each test case in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^6
-10^3 <= 'ARR'[i] <= 10^3
Time limit: 1 sec
2
5
6 7 1 8 5
3
6 6 6
1
6
For the first test case,
We can split the array into [6,7] and [1,8,5]. The absolute difference is |13-14| =1.
Hence, the answer is 1.
For the second test case,
We can split the array into [6] and [6,6]. The absolute difference is |6-12| =6.
Hence, the answer is 6.
2
5
4 1 20 9 9
4
7 9 8 10
7
2
Can you store the sum of subarrays and check for the minimum difference?
In this approach, we will first store the prefix sum of the given array 'ARR' into an array 'PREFIXSUM'. 'PREFIXSUM'[i] denotes the sum of all elements from index 0 to i.
Now, We will compute the sum of both the subarrays using the 'PREFIXSUM' array and compare the minimum absolute difference.
Algorithm:
O(N), where N is the number of elements in 'ARR'.
In this approach, we are traversing the array to compute the prefix sum and to compute the minimum absolute difference. Hence the overall time complexity is O(N).
O(N), where N is the number of elements in 'ARR'.
We are using a 'PREFIXSUM' array to store the prefix sum of the given array. Hence the overall space complexity is O(N).