Equal Sums
Easy Accuracy: 40.2% Submissions: 599 Points: 2

Given an array A of positive integers, find the smallest non-negative integer (i.e. greater than or equal to zero) that can be placed between any two elements of the array such that the sum of elements in the subarray occurring before it,  is equal to the sum of elements occurring in the subarray after it, with the newly placed integer included in either of the two subarrays.


The first line of input is an integer T, denoting the number of test cases. For each test case, there are two subsequent lines of input. First line consists of an integer N, size of the integer array. Second line consists of N space separated integers of array A.


For each test case there is only one line of input comprising of three space separated integers. First, the new number that can be inserted. Second, the position (starting from 2 to N-1 , because new number can only be inserted between any two existing elements) where it should be inserted. Third, the subarray in which it is included (1 or 2) to get equal sums. If the smallest integer to be inserted has more than one possible position in the array to get equal sums, print the smallest position out of all possibilities. In case equal sums are obtained without adding any new element, the value of new element will be zero, followed by the position number, and is always included in the first subarray.

Note: Position of first element of array should be considered as 1.




0<=A[i]<=50, where i is an integer such that 0<=i



3 2 1 5 7 8
9 5 1 2 0
3 2 2 3


4 5 1
1 2 2
0 3 1


In the first test case, the smallest possible number that we can insert is 4, at position 5 (i.e. between 5 and 7) as part of first subarray so that the sum of the two subarrays becomes equal as, 3+2+1+5+4=15 and 7+8=15.

Similarly in the second test case, the smallest possible number that we can insert is 1, at position 2 (i.e. between 9 and 5) as part of second subarray in order to get an equal sum of 9.

In the third test case, equal sum of 5 is obtained by adding first two elements and last two elements as separate subarrays without inserting any extra number. Hence, the output is zero (least possible non-negative integer), followed by 3, the position number and as part of first subarray.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.