Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes.
Given an array, your task is to find the index of first Equilibrium point in the array.
Input Format:
The first line of input takes an integer T denoting the no of test cases, then T test cases follow. The first line of each test case is an integer N denoting The size of the array. Then in the next line are N space-separated values of the array. 
Output Format:
For each test case, the output will be the equilibrium index of the array. If no such index exists output will be -1.
User Task :
Your task is to complete the function (findEquilibrium) below which returns the index of the first Equilibrium point in the array. The function takes two arguments. The first argument is the array A[ ] and the second argument is the size of the array A.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1<=T<=100
1<=N<=10^{5}
-1000<=A[]<=1000
Example(To be used only for expected output):
Input
2
4
1 2 0 3
4
1 1 1 1
Output
2
-1
Note: The Input/Output format and Examples given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: # Your task is to ocomplete this function

# Function should return an integer

def findEquilibrium(a,n):

    # Code here



Solutions:
def findEquilibrium(a, n):
	lsum = 0
	rsum = 0
	for i in range(0, n):
		rsum = rsum + a[i]
	for i in range(0, n):
		rsum = rsum - a[i]
		if rsum == lsum:
			return i
		else:
			lsum = lsum + a[i]
	return -1


def findEquilibrium(a, n):
	left_sum = 0
	total_sum = sum(a)
	for i in range(n):
		total_sum -= a[i]
		if left_sum == total_sum:
			return i
		left_sum += a[i]
	return -1


def findEquilibrium(a, n):
	P = []
	S = []
	p = 0
	q = 0
	for i in range(n):
		p += a[i]
		P.append(p)
	for i in range(n - 1, -1, -1):
		q += a[i]
		S.append(q)
	S = S[::-1]
	if n == 1 or n == 2:
		return -1
	for i in range(1, n - 1):
		if P[i - 1] == S[i + 1]:
			return i
	return -1


def findEquilibrium(a, n):
	sm = sum(a)
	csm = 0
	for i in range(n):
		if csm == sm - csm - a[i]:
			return i
		csm += a[i]
	return -1


def findEquilibrium(a, n):
	s = sum(a)
	s1 = 0
	for i in range(n):
		if s - s1 - a[i] == s1:
			return i
		s1 += a[i]
	return -1


def findEquilibrium(a, n):
	l = 0
	r = sum(a)
	for i in range(len(a)):
		r -= a[i]
		if l == r:
			return i
		l += a[i]
	return -1


def findEquilibrium(a, n):
	s = sum(a)
	ls = 0
	for i in range(n):
		if ls == s - arr[i] - ls:
			return i
		ls = ls + arr[i]
	return -1


def findEquilibrium(a, n):
	total = sum(a)
	leftSum = 0
	for i in range(len(a)):
		rightSum = total - leftSum - a[i]
		if leftSum == rightSum:
			return i
		leftSum += a[i]
	return -1


def findEquilibrium(a, n):
	rt_sum = 0
	left_sum = 0
	for i in range(n):
		rt_sum += a[i]
	for i in range(n):
		rt_sum -= a[i]
		if rt_sum == left_sum:
			return i
		left_sum += a[i]
	return -1


def findEquilibrium(a, n):
	s1 = 0
	s2 = sum(a)
	for i in range(n):
		if s1 == s2 - s1 - arr[i]:
			return i
		s1 += arr[i]
	return -1


def findEquilibrium(a, n):
	sum_till = [0]
	s = 0
	for elem in a:
		s += elem
		sum_till.append(s)
	for index in range(1, n):
		if sum_till[index - 1] == s - sum_till[index]:
			return index - 1
	return -1


def findEquilibrium(a, n):
	for i in range(n):
		ts = sum(a)
		ls = 0
		for (i, ele) in enumerate(a, 0):
			ts = ts - ele
			if ls == ts:
				return i
			ls = ls + ele
		return -1


def findEquilibrium(a, n):
	sm = sum(a)
	leftsm = 0
	ans = -1
	for i in range(0, len(a)):
		rightsm = sm - leftsm - a[i]
		if leftsm == rightsm:
			ans = i
			break
		leftsm = leftsm + a[i]
	if ans != -1:
		return ans
	return -1


def findEquilibrium(a, n):
	tSum = sum(a)
	(lSum, rSum) = (0, 0)
	for i in range(0, n):
		rSum = tSum - lSum - a[i]
		if lSum == rSum:
			return i
		else:
			lSum += a[i]
	else:
		return -1


def findEquilibrium(a, n):
	sum = 0
	leftSum = 0
	for i in range(n):
		sum += a[i]
	for i in range(n):
		sum -= a[i]
		if leftSum == sum:
			return i
		leftSum += a[i]
	return -1


def findEquilibrium(a, n):
	left = [0] * n
	right = [0] * n
	left[0] = a[0]
	right[-1] = a[-1]
	for i in range(1, n):
		left[i] += left[i - 1] + a[i]
	for i in range(n - 2, -1, -1):
		right[i] = right[i + 1] + a[i]
	for i in range(1, n - 1):
		if left[i - 1] == right[i + 1]:
			return i
	return -1


def findEquilibrium(a, n):
	left = 0
	right = n - 1
	leftsum = a[0]
	rightsum = a[right]
	while left < right:
		if leftsum <= rightsum:
			left += 1
			leftsum += a[left]
		elif leftsum > rightsum:
			right -= 1
			rightsum += a[right]
		if right - left == 2:
			if leftsum == rightsum:
				return left + 1
			else:
				return -1


def findEquilibrium(a, n):
	rs = sum(a)
	ls = 0
	for i in range(0, n):
		rs -= a[i]
		if rs == ls:
			return i
		ls += a[i]
	return -1


def findEquilibrium(a, n):
	rsum = sum(a)
	left = 0
	for (index, num) in enumerate(a):
		rsum -= num
		if left == rsum:
			return index
		left += num
	return -1


def findEquilibrium(a, n):
	res = -1
	left_sum = [0] * n
	right_sum = [0] * n
	l_sum = 0
	for i in range(n):
		l_sum += a[i]
		left_sum[i] = l_sum
	r_sum = 0
	for i in range(n - 1, -1, -1):
		r_sum += a[i]
		right_sum[i] = r_sum
	for i in range(n):
		if left_sum[i] == right_sum[i]:
			return i
	return res


def findEquilibrium(a, n):
	(right_sum, left_sum) = (0, 0)
	for i in range(1, n):
		right_sum += arr[i]
	(i, j) = (0, 1)
	while j < n:
		right_sum -= arr[j]
		left_sum += arr[i]
		if left_sum == right_sum:
			return i + 1
		j += 1
		i += 1
	return -1


def findEquilibrium(a, n):
	sum = 0
	suml = 0
	for i in range(n):
		sum = sum + a[i]
	for j in range(n):
		sum = sum - a[j]
		if suml == sum:
			return j
		else:
			suml = suml + a[j]
	return -1


def findEquilibrium(a, n):
	totalsum = sum(a)
	leftsum = 0
	for i in range(n):
		rightsum = totalsum - a[i] - leftsum
		if leftsum == rightsum:
			return i
		leftsum += a[i]
	return -1


def findEquilibrium(a, n):
	rsum = sum(arr)
	lsum = 0
	for i in range(n - 1, -1, -1):
		rsum -= a[i]
		if rsum == lsum:
			return i
		lsum += a[i]
	return -1


def findEquilibrium(a, n):
	s1 = 0
	s2 = 0
	i = 0
	j = n - 1
	while i < j:
		if s2 < s1:
			s2 = s2 + a[j]
			j = j - 1
		else:
			s1 = s1 + a[i]
			i = i + 1
	if s1 == s2:
		return i
	else:
		return -1


def findEquilibrium(a, n):
	ps = [a[0]]
	for i in range(1, n):
		ps.append(ps[-1] + a[i])
	for i in range(1, len(ps)):
		if ps[i - 1] == ps[-1] - ps[i]:
			return i
	return -1


def findEquilibrium(A, N):
	larr = 0
	rarr = sum(A) - A[0]
	if len(A) == 1:
		return 0
	for i in range(0, N - 1):
		if larr == rarr:
			return i
		larr += A[i]
		rarr -= A[i + 1]
	return -1


def findEquilibrium(a, n):
	sum = 0
	for i in a:
		sum += i
	leftsum = 0
	for i in range(len(a)):
		sum -= a[i]
		if leftsum == sum:
			return i
		leftsum += a[i]
	return -1


def findEquilibrium(a, n):
	if n == 1:
		return 0
	if n < 1:
		return -1
	curr_sum = 0
	total = sum(arr)
	for i in range(1, n):
		curr_sum += arr[i - 1]
		if curr_sum * 2 == total - arr[i]:
			return i
	return -1


def findEquilibrium(A, N):
	s = sum(A)
	pre = 0
	if N == 1:
		return 1
	for i in range(1, N):
		pre += A[i - 1]
		if s - A[i] - pre == pre:
			return i
	return -1


def findEquilibrium(arr, n):
	totalSum = sum(arr)
	right = totalSum
	left = 0
	for i in range(len(arr)):
		nums = arr[i]
		right -= nums
		if left == right:
			return i
		left += nums
	return -1


def findEquilibrium(a, n):
	tot = sum(a)
	cumSum = a[0]
	for i in range(1, n):
		if (tot - arr[i]) / 2 == cumSum:
			return i
		else:
			cumSum = cumSum + a[i]
	return -1


def findEquilibrium(a, n):
	s = sum(a)
	newSum = a[0]
	s -= newSum
	for i in range(1, n):
		temp = s - a[i]
		if temp == newSum:
			return i
		else:
			newSum += a[i]
			s -= a[i]
	return -1


def findEquilibrium(a, n):
	s = sum(a)
	leftsum = 0
	for i in range(n):
		s -= a[i]
		if s == leftsum:
			return i
		else:
			leftsum += a[i]
	return -1


def findEquilibrium(a, n):
	(first, second) = (0, sum(a))
	for i in range(n - 1):
		second -= a[i]
		if first == second:
			return i
		first += a[i]
	return -1


def findEquilibrium(arr, n):
	val = -1
	for i in range(1, n):
		arr[i] = arr[i] + arr[i - 1]
	for i in range(1, n):
		if arr[i - 1] == arr[-1] - arr[i]:
			val = i
	return val


def findEquilibrium(a, n):
	total_sum = sum(a)
	cs = 0
	for i in range(n):
		if cs == (total_sum - a[i]) / 2:
			return i
		cs += a[i]
	else:
		return -1


def findEquilibrium(arr, n):
	total = sum(arr)
	temp = arr[0]
	for i in range(1, n):
		if (total - arr[i]) / 2 == temp:
			return i
		temp += arr[i]
	return -1


def findEquilibrium(a, n):
	total_sum = sum(a)
	logical = 1
	sum_left = 0
	for i in range(1, n):
		sum_left = sum_left + a[i - 1]
		if (total_sum - a[i]) % 2 == 0:
			if sum_left == (total_sum - a[i]) / 2:
				return i
				logical = 0
				break
	if logical == 1:
		return -1


def findEquilibrium(a, n):
	sum1 = sum(a)
	sum2 = 0
	while True:
		if sum1 == sum2 * 2 + a[-1]:
			return len(a) - 1
		if sum2 * 2 > sum1:
			return -1
		sum2 += a.pop()


def findEquilibrium(a, n):
	prefix = [0] * len(a)
	prefix[0] = a[0]
	for i in range(len(a)):
		prefix[i] = prefix[i - 1] + a[i]
	suffix = [0] * len(a)
	suffix[-1] = a[-1]
	for j in range(len(a) - 2, -1, -1):
		suffix[j] = suffix[j + 1] + a[j]
	for i in range(len(a)):
		left_sum = 0
		right_sum = 0
		if i == 0:
			left_sum = 0
			right_sum = suffix[i + 1]
		elif i == len(a) - 1:
			right_sum = 0
			left_sum = prefix[i - 1]
		else:
			left_sum = prefix[i - 1]
			right_sum = suffix[i + 1]
		if left_sum == right_sum:
			return i
	return -1


def findEquilibrium(a, n):
	g = 0
	su = sum(a)
	for x in range(1, len(a)):
		g = g + a[x - 1]
		if 2 * g + a[x] == su:
			return x
	return -1


from sys import stdin, stdout

def findEquilibrium(l, n):
	p_sum = [0] * n
	p_sum[0] = l[0]
	for i in range(1, n):
		p_sum[i] = p_sum[i - 1] + l[i]
	index = -1
	(l1, r) = (0, 0)
	if n == 0:
		return -1
	else:
		for i in range(n):
			if i == 0:
				l1 = 0
				r = p_sum[n - 1] - p_sum[i]
			else:
				l1 = p_sum[i - 1]
				r = p_sum[n - 1] - p_sum[i]
			if l1 == r:
				index = max(index, i)
		return index


def findEquilibrium(a, n):
	(l, h, s1, s2) = (0, n - 1, 0, 0)
	while l < h:
		if s1 > s2:
			s2 = s2 + a[h]
			h = h - 1
		else:
			s1 = s1 + a[l]
			l = l + 1
	if s1 == s2:
		return l
	else:
		return -1


def findEquilibrium(a, n):
	if n == 0:
		return -1
	else:
		l = a
		prefix = []
		index = -1
		s = 0
		f = 0
		for i in a:
			s = s + i
			prefix.append(s)
		for i in range(len(prefix)):
			if i == 0:
				l = 0
				r = prefix[-1] - prefix[i]
			else:
				l = prefix[i - 1]
				r = prefix[len(prefix) - 1] - prefix[i]
			if l == r:
				f = 1
				index = max(index, i)
		if f == 0:
			return -1
		else:
			return index


def findEquilibrium(a, n):
	x = sum(a)
	j = 0
	for i in range(n):
		j += a[i]
		if (x - a[i]) / 2 == j - a[i]:
			return i
	return -1


def findEquilibrium(a, n):
	n = len(a)
	prefixSum = []
	sum = 0
	for i in range(n):
		sum = sum + a[i]
		prefixSum.append(sum)
	for j in range(n):
		eqb = a[j]
		if j == 0:
			lsum = 0
		else:
			lsum = prefixSum[j - 1]
		if j == n - 1:
			rsum = 0
		else:
			rsum = prefixSum[n - 1] - prefixSum[j]
		if lsum == rsum:
			return j
	return -1


def findEquilibrium(a, n):
	leftSum = 0
	rightSum = sum(a)
	for (index, value) in enumerate(a):
		rightSum -= value
		if rightSum == leftSum:
			return index
		leftSum += value
	return -1


def findEquilibrium(arr, n):
	if len(arr) == 1:
		return 0
	left_prefix = [0] * len(arr)
	left_prefix[0] = arr[0]
	for i in range(1, len(arr)):
		left_prefix[i] = left_prefix[i - 1] + arr[i]
	right_prefix = [0] * len(arr)
	right_prefix[-1] = arr[-1]
	for i in range(len(arr) - 2, -1, -1):
		right_prefix[i] = right_prefix[i + 1] + arr[i]
	left_sum = 0
	right_sum = 0
	for i in range(len(arr)):
		if i == 0:
			left_sum = 0
			right_sum = right_prefix[i + 1]
		elif i == len(arr) - 1:
			left_sum = left_prefix[i - 1]
			right_sum = 0
		else:
			left_sum = left_prefix[i - 1]
			right_sum = right_prefix[i + 1]
		if left_sum == right_sum:
			return i
	return -1


def findEquilibrium(a, n):
	sum1 = 0
	for i in range(n):
		sum1 = sum1 + a[i]
	index = -1
	sum2 = 0
	value = 0
	for j in range(n - 1, -1, -1):
		value = sum1 - sum2
		sum2 += a[j]
		if value == sum2:
			index = j
	return index


def findEquilibrium(a, n):
	x = sum(a)
	star = 0
	for i in range(len(a)):
		star = star + a[i]
		y = star - a[i]
		z = x - y
		if star == z:
			return i
	return -1


def findEquilibrium(a, n):
	arrSum = sum(a)
	leftSum = 0
	for i in range(len(a)):
		rightSum = arrSum - leftSum - a[i]
		if leftSum == rightSum:
			return i
		leftSum += a[i]
	else:
		return -1


def findEquilibrium(a, n):
	total_sum = sum(a)
	(left_sum, cur_sum) = (0, 0)
	for i in range(len(a)):
		cur_sum += a[i]
		right_sum = total_sum - cur_sum
		if left_sum == right_sum:
			return i
		left_sum += a[i]
	return -1


def findEquilibrium(a, n):
	if n <= 2:
		return -1
	cl = arr[0]
	rl = sum(arr) - cl - a[1]
	if cl == rl:
		return 1
	for i in range(2, n):
		cl += arr[i - 1]
		rl -= arr[i]
		if cl == rl:
			return i
	return -1


def findEquilibrium(a, n):
	total = sum(arr)
	leftsum = 0
	for (i, num) in enumerate(arr):
		total -= num
		if leftsum == total:
			return i
		leftsum += num
	return -1


def findEquilibrium(a, n):
	leftsum = []
	rightsum = []
	for i in range(n):
		if i:
			leftsum.append(leftsum[i - 1] + arr[i])
			rightsum.append(rightsum[i - 1] + arr[n - 1 - i])
		else:
			leftsum.append(arr[i])
			rightsum.append(arr[n - 1 - i])
	for i in range(n):
		if leftsum[i] == rightsum[n - 1 - i]:
			return i
	return -1


def findEquilibrium(a, n):
	pivot = 0
	sum1 = 0
	for i in range(len(a)):
		sum1 = sum1 + a[i]
	for i in range(len(a)):
		if pivot == sum1 - pivot - arr[i]:
			return i
		pivot += a[i]
	return -1


def findEquilibrium(a, n):
	right_sum = sum(arr)
	left_sum = 0
	for (index, num) in enumerate(arr):
		right_sum -= num
		if left_sum == right_sum:
			return index
		left_sum += num
	return -1


def findEquilibrium(a, n):
	sumA = a[0]
	sumB = a[-1]
	x = 0
	y = n - 1
	while x <= y:
		if sumA < sumB:
			sumA += a[x + 1]
			x += 1
		elif sumA > sumB:
			sumB += a[y - 1]
			y -= 1
		elif sumA == sumB and x == y - 2:
			return x + 1
		else:
			sumA += a[x + 1]
			sumB += a[y - 1]
			x += 1
			y -= 1
	return -1


def findEquilibrium(a, n):
	p = [0] * n
	p[0] = a[0]
	n = len(a)
	for i in range(1, n):
		p[i] = p[i - 1] + a[i]
	s = [0] * n
	s[n - 1] = a[n - 1]
	for i in range(n - 2, -1, -1):
		s[i] = s[i + 1] + a[i]
	for i in range(1, n - 1):
		if s[i + 1] == p[i - 1]:
			return i
	return -1


def findEquilibrium(a, n):
	preFix = []
	postFix = []
	temp = 0
	for i in a:
		preFix.append(temp)
		temp += i
	temp = 0
	for i in range(len(a) - 1, -1, -1):
		postFix.append(temp)
		temp += a[i]
	postFix = postFix[::-1]
	for i in range(len(preFix)):
		if preFix[i] == postFix[i]:
			return i
	return -1


def findEquilibrium(a, n):
	ts = sum(a)
	l = 0
	r = ts - l
	for i in range(1, n):
		if l != r:
			l = l + a[i - 1]
			r = ts - l - a[i]
		else:
			return i - 1
	return -1


def findEquilibrium(a, n):
	summ = sum(a)
	left = 0
	for (i, num) in enumerate(a):
		summ -= num
		if summ == left:
			return i
		left += num
	return -1


def findEquilibrium(a, n):
	ts = sum(a)
	l = 0
	for (i, n) in enumerate(a):
		ts -= n
		if ts == l:
			return i
		l += n
	return -1


def findEquilibrium(a, n):
	left = 0
	right = sum(a)
	for i in range(n):
		left += arr[i]
		if left == right:
			return i
		right -= arr[i]
	else:
		return -1


def findEquilibrium(a, n):
	arr = []
	lst = []
	s = 0
	for i in range(n):
		s += a[i]
		arr.append(s)
	m = 0
	for j in range(n):
		m += a[n - j - 1]
		lst.append(m)
	lst = lst[::-1]
	for i in range(n):
		if arr[i] - a[i] == lst[i] - a[i]:
			return i
	else:
		return -1


def findEquilibrium(a, n):
	ts = sum(a)
	left_sum = 0
	for (i, num) in enumerate(a):
		ts -= num
		if left_sum == ts:
			return i
		left_sum += num
	return -1


def findEquilibrium(a, n):
	sum1 = 0
	sum2 = sum(arr[1:])
	for i in range(n):
		if i > 0:
			sum1 += a[i - 1]
			sum2 -= a[i]
		if sum1 == sum2:
			return i
	return -1