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.
 
Example 1:
Input : Arr[] = {3, 2, 1, 5, 7, 8}
Output : 4 5 1
Explanation:
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.
Example 2:
Input : Arr[] = {9, 5, 1, 2, 0}
Output : 1 2 2
Explanation:
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.
 
Output:
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.
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function EqualSum() that takes an array (arr), sizeOfArray (n), and return the array of 3 values define in the output format. The driver code takes care of the printing.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Note: Position of first element of array should be considered as 1.
Constraints:
2 ≤ N ≤ 10^{4}
0 ≤ A[i] ≤ 10^{3}, where i is an integer such that 0 ≤ i

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3

class Solution:
    def EqualSum(self,a,n):
        #Complete the function


Solutions:
class Solution:

	def EqualSum(self, a, n):
		summa = sum(a)
		diff = float('inf')
		index = 0
		pos = 1
		for (i, item) in enumerate(a):
			summa -= item * 2
			if abs(summa) < diff:
				diff = abs(summa)
				index = i + 2
				if summa >= 0:
					pos = 1
				else:
					pos = 2
		return (diff, index, pos)


class Solution:

	def EqualSum(self, a, n):
		if len(a) == 1:
			return [a[0], 2, 2]
		sum_a = []
		s = 0
		for i in a:
			s += i
			sum_a.append(s)
		full_arr_sum = sum_a[-1]
		ans = [-1, -1, -1]
		min_no = float('inf')
		for i in range(1, n):
			left_sum = sum_a[i - 1]
			right_sum = full_arr_sum - left_sum
			diff = abs(left_sum - right_sum)
			if min_no > diff:
				if left_sum > right_sum:
					ans[0] = diff
					ans[1] = i + 1
					ans[2] = 2
				else:
					ans[0] = diff
					ans[1] = i + 1
					ans[2] = 1
				min_no = diff
		return ans


class Solution:

	def EqualSum(self, a, n):
		i = 0
		j = n - 1
		sum1 = 0
		sum2 = a[j]
		if n == 1:
			return (a[0], 2, 2)
		while i < j:
			if sum1 < sum2:
				sum1 += a[i]
				i += 1
			else:
				j -= 1
				sum2 += a[j]
		if sum1 > sum2:
			return (sum1 - sum2, j + 1, 2)
		else:
			return (sum2 - sum1, i + 1, 1)


class Solution:

	def EqualSum(self, a, n):
		for i in range(1, n):
			a[i] = a[i - 1] + a[i]
		l = []
		r = []
		for i in range(n):
			k = a[n - 1] - a[i] - a[i]
			if k >= 0:
				l.append(abs(k))
				r.append(100000000)
			else:
				r.append(abs(k))
				l.append(100000000)
		(c, d, e) = (0, 0, 1)
		c = min(l + r)
		if c in l:
			d = l.index(c)
		else:
			d = r.index(c)
			e = 2
		return (c, d + 2, e)


class Solution:

	def EqualSum(self, a, n):
		i = 0
		j = n - 1
		sum1 = 0
		sum2 = a[j]
		if n == 1:
			return (a[0], 2, 2)
		while i < j:
			if sum1 < sum2:
				sum1 += a[i]
				i += 1
			else:
				j -= 1
				sum2 += a[j]
		if sum1 > sum2:
			return (sum1 - sum2, j + 1, 2)
		else:
			return (sum2 - sum1, i + 1, 1)
for _ in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	ob = Solution()
	v = ob.EqualSum(arr, n)
	print(str(v[0]) + ' ' + str(v[1]) + ' ' + str(v[2]))


class Solution:

	def EqualSum(self, a, n):
		lsum = []
		rsum = [0] * n
		sum1 = 0
		sum2 = 0
		min1 = 10 ** 12
		x = 0
		if n == 1:
			return (a[0], 2, 2)
		for i in range(n):
			sum1 += a[i]
			lsum.append(sum1)
		for i in range(n - 1, -1, -1):
			sum2 += a[i]
			rsum[i] = sum2
		for i in range(n - 1):
			if abs(lsum[i] - rsum[i + 1]) < min1:
				min1 = abs(lsum[i] - rsum[i + 1])
				x = i
		if lsum[x] > rsum[x + 1]:
			return (min1, x + 2, 2)
		else:
			return (min1, x + 2, 1)
for _ in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	ob = Solution()
	v = ob.EqualSum(arr, n)
	print(str(v[0]) + ' ' + str(v[1]) + ' ' + str(v[2]))


class Solution:

	def EqualSum(self, a, n):
		sm = 0
		s = 0
		for i in range(0, n):
			sm += a[i]
		ans1 = 1000000000
		ans2 = 0
		ans3 = 0
		for i in range(0, n):
			s += a[i]
			sm -= s
			k = abs(sm - s)
			if sm > s:
				if k < ans1:
					ans1 = k
					ans3 = 1
					ans2 = i + 2
			elif k < ans1:
				ans1 = k
				ans3 = 2
				ans2 = i + 2
			sm += s
		v = list()
		if ans1 == 0:
			ans3 = 1
		v.append(ans1)
		v.append(ans2)
		v.append(ans3)
		return v