Given an array A of n positive numbers. The task is to find the first index in the array such that the sum of elements before it is equal to the sum of elements after it.
Note:  Array is 1-based indexed.
Example 1:
Input: 
n = 5 
A[] = {1,3,5,2,2} 
Output: 3 
Explanation: For second test case 
at position 3 elements before it 
(1+3) = elements after it (2+2). 
 
Example 2:
Input:
n = 1
A[] = {1}
Output: 1
Explanation:
Since its the only element hence
it is the only point.
Your Task:
The task is to complete the function equalSum() which takes the array and n as input parameters and returns the point. Return -1 if no such point exists. 
Expected Time Complexily: O(N)
Expected Space Complexily: O(1)
Constraints:
1 <= n <= 10^{6}
1 <= A[i] <= 10^{8}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Range queries

Starter code: # User function Template for python3



class Solution:

    # Complete this function

    

    #Function to find equilibrium point in the array.

    def equalSum(self,A, N):

        # Your code here



Solutions:
class Solution:

	def equalSum(self, A, N):
		rightsum = 0
		i = N - 1
		leftsum = sum(A) - A[i]
		while i >= 0:
			if rightsum != leftsum:
				rightsum += A[i]
				leftsum -= A[i - 1]
				i -= 1
			else:
				return i + 1
		return -1


class Solution:

	def equalSum(self, A, N):
		if N == 1:
			return 1
		sum = 0
		for i in A:
			sum += i
		leftsum = 0
		for ind in range(0, N):
			if leftsum == sum - leftsum - A[ind]:
				return ind + 1
			leftsum = leftsum + A[ind]
		return -1


class Solution:

	def equalSum(self, A, N):
		total = 0
		for i in A:
			total += i
		left = 0
		right = total
		for i in range(N):
			right -= A[i]
			if left == right:
				return i + 1
			else:
				left += A[i]
		return -1


class Solution:

	def equalSum(self, A, N):
		left_sum = 0
		totalsum = sum(A)
		for i in range(N):
			totalsum -= A[i]
			if totalsum == left_sum:
				return i + 1
			left_sum += A[i]
		return -1


class Solution:

	def equalSum(self, A, N):
		if N == 1:
			return 1
		l_sum = A[0]
		r_sum = sum(A[2:])
		for i in range(1, N - 1):
			if l_sum == r_sum:
				return i + 1
			else:
				l_sum += A[i]
				r_sum -= A[i + 1]
		return -1


class Solution:

	def equalSum(self, A, N):
		(totalSum, leftSum) = (0, 0)
		for num in A:
			totalSum += num
		for i in range(N):
			totalSum -= A[i]
			if totalSum == leftSum:
				return i + 1
			leftSum += A[i]
		return -1


class Solution:

	def equalSum(self, A, N):
		summ = 0
		lsumm = 0
		for i in range(N):
			summ += A[i]
		for i in range(N):
			summ -= A[i]
			if lsumm == summ:
				return i + 1
			lsumm += A[i]
		return -1


class Solution:

	def equalSum(self, a, n):
		ts = sum(a)
		cs = 0
		for i in range(n):
			cs = cs + a[i]
			if cs == ts:
				return i + 1
			else:
				ts = ts - a[i]
		return -1


class Solution:

	def equalSum(self, a, N):
		ls = 0
		rs = 0
		for i in range(N):
			rs += a[i]
		for i in range(N):
			rs -= a[i]
			if rs == ls:
				return i + 1
			ls += a[i]
		return -1


class Solution:

	def equalSum(self, A, N):
		if N == 1:
			return 1
		p = []
		p.append(A[0])
		for i in range(1, N):
			p.append(p[i - 1] + A[i])
		for i in range(N - 1):
			ls = p[i]
			rs = p[N - 1] - p[i + 1]
			if ls == rs:
				return i + 2
		return -1


class Solution:

	def equalSum(self, A, N):
		if N == 1:
			return 1
		(l, r) = (0, N - 1)
		(s_l, s_r) = (A[l], A[r])
		ans = -1
		while l < r:
			if s_l == s_r and l + 1 == r - 1:
				ans = l + 2
				break
			elif s_l < s_r:
				l += 1
				s_l += A[l]
			else:
				r -= 1
				s_r += A[r]
		return ans


class Solution:

	def equalSum(self, A, N):
		if N == 0:
			return 0
		if N == 1:
			return 1
		sum = 0
		for n in A:
			sum += n
		left_sum = 0
		right_sum = sum - A[0]
		for i in range(len(A) - 1):
			if left_sum == right_sum:
				return i + 1
			else:
				left_sum += A[i]
				right_sum -= A[i + 1]
		if sum - A[-1] == 0:
			return N
		return -1


class Solution:

	def equalSum(self, A, N):
		r = 0
		x = sum(A)
		for i in range(N):
			x -= A[i]
			if x == r:
				return i + 1
			r += A[i]
		return -1


class Solution:

	def equalSum(self, A, N):
		sum1 = 0
		sum2 = 0
		if N == 1:
			return 1
		for i in range(1, N):
			sum2 += A[i]
		for i in range(0, N - 1):
			if sum1 == sum2:
				return i + 1
			sum1 += A[i]
			sum2 -= A[i + 1]
		return -1


class Solution:

	def equalSum(self, A, N):
		leftSum = [0] * N
		rightSum = [0] * N
		for i in range(1, N):
			leftSum[i] = leftSum[i - 1] + A[i - 1]
		for i in range(N - 2, -1, -1):
			rightSum[i] = rightSum[i + 1] + A[i + 1]
		for i in range(N):
			if rightSum[i] == leftSum[i]:
				return i + 1
		return -1


class Solution:

	def equalSum(self, A, N):
		a = 0
		b = 0
		for i in range(N):
			a = a + A[i]
		a = a - A[0]
		if N == 2:
			return -1
		elif N == 1:
			return 1
		else:
			for i in range(N - 2):
				b = b + A[i]
				a = a - A[i + 1]
				if a == b:
					return i + 2
		return -1


class Solution:

	def equalSum(self, A, N):
		dp = [A[0]]
		dpp = [A[N - 1]]
		for i in range(1, N):
			sum = 0
			sum = A[i] + dp[i - 1]
			dp.append(sum)
		for i in range(1, N):
			sum = 0
			sum = A[N - 1 - i] + dpp[i - 1]
			dpp.append(sum)
		summ = 0
		if N > 2:
			for i in range(N - 2):
				summ = 0
				if dp[i] == dpp[N - i - 3]:
					summ = i + 2
					break
				else:
					summ = -1
			return summ
		elif N == 2:
			return -1
		else:
			return 1


class Solution:

	def equalSum(self, A, N):
		if N == 1:
			return 1
		prefix_sum = [0] * len(A)
		if len(A) == 1:
			return 0
		for i in range(1, len(A)):
			prefix_sum[i] = prefix_sum[i - 1] + A[i - 1]
		suffix_sum = [0] * len(A)
		A = A[::-1]
		for i in range(1, len(A)):
			suffix_sum[i] = suffix_sum[i - 1] + A[i - 1]
		suffix_sum = suffix_sum[::-1]
		for i in range(0, len(A)):
			if prefix_sum[i] == suffix_sum[i]:
				return i + 1
		return -1


class Solution:

	def equalSum(self, A, N):
		r_sum = 0
		s_sum = sum(A[:])
		for i in range(N):
			if i != 0:
				r_sum += A[i - 1]
			s_sum -= A[i]
			if r_sum == s_sum:
				return i + 1
		return -1


class Solution:

	def equalSum(self, A, N):
		p_sum = 0
		a_sum = sum(A[:])
		for i in range(N):
			if i != 0:
				p_sum += A[i - 1]
			a_sum -= A[i]
			if p_sum == a_sum:
				return i + 1
		return -1


class Solution:

	def equalSum(self, A, N):
		if N == 1:
			return 1
		total_sum = 0
		right_sum = 0
		left_sum = 0
		for i in range(N):
			total_sum = total_sum + A[i]
		for i in range(N):
			right_sum = total_sum - left_sum - A[i]
			if right_sum == left_sum:
				return i + 1
				break
			left_sum = left_sum + A[i]
		else:
			return -1


class Solution:

	def equalSum(self, A, N):
		initial = 0
		final = sum(A)
		index = 0
		for i in range(N):
			if i == 0:
				initial = 0
			else:
				initial += A[i - 1]
			final -= A[i]
			if initial == final:
				index = i + 1
				break
		if index == 0:
			return -1
		else:
			return index


class Solution:

	def equalSum(self, A, N):
		l = 0
		r = 0
		for i in range(N):
			r += A[i]
		for i in range(N):
			r -= A[i]
			if l == r:
				return i + 1
			l += A[i]
		return -1


class Solution:

	def equalSum(self, A, N):
		sums1 = sum(A)
		sums2 = 0
		for i in range(N):
			sums1 = sums1 - A[i]
			if sums1 == sums2:
				return i + 1
			sums2 += A[i]
		return -1


class Solution:

	def equalSum(self, A, N):
		if len(A) == 1:
			return 1
		(totl, ad) = (0, 0)
		totl = sum(A)
		for i in range(N):
			totl -= A[i]
			if totl == ad:
				return i + 1
			ad += A[i]
		return -1


class Solution:

	def equalSum(self, A, N):
		s = 0
		c = 0
		for i in range(0, N):
			s = s + A[i]
		for i in range(0, N):
			if i > 0:
				c = c + A[i - 1]
			s = s - A[i]
			if c == s:
				return i + 1
		return -1


class Solution:

	def equalSum(self, A, N):
		k = sum(A)
		y = 0
		for i in range(N):
			g = k - A[i]
			if g == y:
				return i + 1
			else:
				k = g
				y = y + A[i]
		else:
			return -1


class Solution:

	def equalSum(self, A, N):
		ac = sum(A)
		cur = 0
		for i in range(N):
			if cur * 2 + A[i] == ac:
				return i + 1
			cur += A[i]
		return -1


class Solution:

	def equalSum(self, A, N):
		t = sum(A)
		ls = 0
		for i in range(N):
			rs = t - ls - A[i]
			if ls == rs:
				return i + 1
			ls += A[i]
		return -1


class Solution:

	def equalSum(self, A, N):
		totalsum = 0
		leftsum = 0
		for i in range(N):
			totalsum += A[i]
		for i in range(N):
			if leftsum == totalsum - leftsum - A[i]:
				return i + 1
			leftsum += A[i]
		return -1


class Solution:

	def equalSum(self, A, N):
		s1 = 0
		s2 = 0
		for I in A:
			s2 += I
		s2 -= A[0]
		if s1 == s2:
			return 1
		for i in range(1, N - 1):
			s1 += A[i - 1]
			s2 -= A[i]
			if s1 == s2:
				return i + 1
		else:
			return -1


class Solution:

	def equalSum(self, A, N):
		a = sum(A)
		mx = 0
		sm = 0
		if N == 1:
			return 1
		for i in range(1, N):
			if mx == sm and mx != 0:
				return i
			else:
				mx = mx + A[i - 1]
				sm = a - mx - A[i]
		return -1


class Solution:

	def equalSum(self, A, N):
		sum_ = 0
		sum_till = []
		for elem in A:
			sum_ += elem
			sum_till.append(sum_)
		for index in range(N):
			left_sum = 0 if index == 0 else sum_till[index - 1]
			right_sum = sum_ - sum_till[index]
			if left_sum == right_sum:
				return index + 1
		return -1


class Solution:

	def equalSum(self, A, N):
		sum = 0
		for i in A:
			sum = sum + i
		if N == 1:
			return 1
		else:
			a = 0
			for i in range(1, N - 1):
				a = a + A[i - 1]
				if a == sum - a - A[i]:
					return i + 1
					break
			else:
				return -1


class Solution:

	def equalSum(self, A, N):
		n = len(A)
		total_sum = sum(A)
		l_sum = 0
		for i in range(len(A)):
			if l_sum == total_sum - l_sum - A[i]:
				return i + 1
			l_sum += A[i]
		return -1


class Solution:

	def equalSum(self, A, N):
		total = sum(A)
		before_sum = A[0]
		if N == 1:
			return 1
		for i in range(1, N):
			if 2 * before_sum == total - A[i]:
				return i + 1
			before_sum += A[i]
		return -1


class Solution:

	def equalSum(self, A, N):
		if len(A) == 1:
			return 1
		if len(A) < 3:
			return -1
		all = len(A)
		lSum = rSum = 0
		l = r = 1
		lSum = A[0]
		rSum = A[-r]
		r = 2
		while l + r < len(A):
			if lSum < rSum:
				lSum += A[l]
				l += 1
			else:
				rSum += A[-r]
				r += 1
		if lSum == rSum:
			return l + 1
		else:
			return -1


class Solution:

	def equalSum(self, A, N):
		arrsum = sum(A)
		currsum = 0
		for i in range(N):
			if currsum == arrsum - currsum - A[i]:
				return i + 1
			currsum += A[i]
		return -1


class Solution:

	def equalSum(self, A, N):
		if N == 1:
			return 1
		if N == 2:
			return -1
		S1 = sum(A) - A[0] - A[1]
		S2 = A[0]
		for i in range(1, N):
			if S1 == S2:
				return i + 1
			else:
				if i == N - 1:
					return -1
				S1 -= A[i + 1]
				S2 += A[i]


class Solution:

	def equalSum(self, A, N):
		ts = sum(A)
		if len(A) == 1:
			return 1
		pre = 0
		suff = 0
		for i in range(1, N):
			pre += A[i - 1]
			suff = ts - pre - A[i]
			if pre == suff:
				return i + 1
		return -1


class Solution:

	def equalSum(self, A, n):
		sumi = sum(A)
		left = 0
		sumi = sumi - A[0]
		if n == 1:
			return 1
		for i in range(0, n - 1):
			if left == sumi:
				return i + 1
			else:
				left += A[i]
				sumi -= A[i + 1]
		return -1


class Solution:

	def equalSum(self, A, N):
		su = sum(A)
		su1 = 0
		for (i, j) in enumerate(A):
			su -= j
			if su == su1:
				return i + 1
			su1 += j
		return -1


class Solution:

	def equalSum(self, arr, N):
		right_sum = sum(arr)
		left_sum = 0
		if N == 1:
			return 1
		for i in range(N):
			right_sum -= arr[i]
			if right_sum == left_sum:
				return i + 1
			left_sum += arr[i]
		return -1


class Solution:

	def equalSum(self, A, N):
		psa = [0] * N
		ssa = [0] * N
		psa[0] = A[0]
		ssa[-1] = A[-1]
		if N == 1:
			return 1
		for i in range(1, N):
			psa[i] = psa[i - 1] + A[i]
		for i in range(N - 2, -1, -1):
			ssa[i] = ssa[i + 1] + A[i]
			if ssa[i] == psa[i]:
				return i + 1
		return -1


class Solution:

	def equalSum(self, A, N):

		def fun(x):
			samplesum = 0
			for y in range(x):
				samplesum += A[y]
			return samplesum
		sum = 0
		for x in A:
			sum += x
		sum2 = sum // 2
		sum3 = sum2
		for x in range(N):
			if sum2 - A[x] > 0:
				sum2 -= A[x]
			elif fun(x) == (sum - A[x]) / 2:
				return x + 1
			else:
				return -1


class Solution:

	def equalSum(self, A, N):
		ts = 0
		ls = 0
		for num in A:
			ts += num
		for i in range(N):
			ts -= A[i]
			if ts == ls:
				return i + 1
			ls += A[i]
		return -1


class Solution:

	def equalSum(self, A, n):
		if n == 1:
			return 1
		tot = sum(A)
		curr = 0
		for p in range(n):
			if curr == tot - A[p]:
				return p + 1
			else:
				tot -= A[p]
				curr += A[p]
		return -1


class Solution:

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


class Solution:

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


class Solution:

	def equalSum(self, A, N):
		low = 0
		mid = 0
		high = N - 1
		while low <= high:
			mid = (high + low) // 2
			if sum(A[:mid + 1]) == sum(A[mid:]):
				return mid + 1
			elif sum(A[:mid + 1]) >= sum(A[mid:]):
				high = mid - 1
			else:
				low = mid + 1
		return -1


class Solution:

	def equalSum(self, A, N):
		if N == 1:
			return 1
		elif N > 1:
			a = 0
			left_sum = 0
			right_sum = sum(A)
			for i in range(N):
				right_sum -= A[i]
				if left_sum == right_sum:
					return i + 1
				left_sum += A[i]
		return -1


class Solution:

	def equalSum(self, A, N):
		if N == 1:
			return 1
		for i in range(1, N):
			A[i] += A[i - 1]
		for i in range(1, N):
			if A[-1] - A[i] == A[i - 1]:
				return i + 1
		return -1


class Solution:

	def equalSum(self, arr, n):
		tot = sum(arr)
		if len(arr) == 1:
			return 1
		ls = 0
		for i in range(1, n - 1):
			ls = ls + arr[i - 1]
			if ls == tot - ls - arr[i]:
				return i + 1
		return -1


class Solution:

	def equalSum(self, A, N):
		if N == 1:
			return 1
		leftsum = -A[-1]
		total = sum(A)
		for i in range(len(A)):
			leftsum += A[i - 1]
			total -= A[i]
			if leftsum == total:
				break
		if i == len(A) - 1:
			return -1
		return i + 1


class Solution:

	def equalSum(self, A, N):
		p_count = sum(A)
		count = 0
		i = 0
		if N == 1:
			return 1
		while i != N:
			if count == p_count - A[i]:
				return i + 1
			else:
				p_count -= A[i]
				count += A[i]
				i += 1
		else:
			return -1


class Solution:

	def equalSum(self, A, N):
		i = 0
		j = N - 1
		(sumi, sumj) = (0, 0)
		while i <= j:
			if sumi > sumj:
				sumj += A[j]
				j -= 1
			elif sumj > sumi:
				sumi += A[i]
				i += 1
			elif i == j:
				return i + 1
			else:
				sumi += A[i]
				sumj += A[j]
				i += 1
				j -= 1
		return -1


class Solution:

	def equalSum(self, A, N):
		if len(A) == 2:
			return -1
		left = 0
		right = sum(A)
		for i in range(len(A)):
			right -= A[i]
			if left == right:
				return i + 1
			else:
				left += A[i]
		return -1


class Solution:

	def equalSum(self, A, N):
		if N == 1:
			return 1
		aSum = sum(A)
		cnt = A[0]
		for i in range(1, N):
			if aSum - cnt - A[i] == cnt:
				return i + 1
			cnt += A[i]
		return -1


class Solution:

	def equalSum(self, a, n):
		temp = 0
		c = 0
		i = int()
		k = sum(a)
		for i in range(0, n):
			f = k - a[i]
			if c == f / 2:
				temp = 1
				break
			else:
				c = c + a[i]
		if temp == 1:
			return i + 1
		else:
			return -1


class Solution:

	def equalSum(self, A, N):
		sum = 0
		sum2 = 0
		for i in range(0, N):
			sum = sum + A[i]
		for j in range(0, N):
			if (sum - A[j]) / 2 == sum2:
				return j + 1
			sum2 = sum2 + A[j]
		return -1


class Solution:

	def equalSum(self, A, N):
		lef = 0
		rig = sum(A)
		f = 0
		for i in range(N):
			rig = rig - A[i]
			if lef == rig:
				f = 1
				return i + 1
			lef = lef + A[i]
		if f == 0:
			return -1


class Solution:

	def equalSum(self, A, N):
		pre = 0
		tsum = sum(A)
		for i in range(N):
			tsum = tsum - A[i]
			if pre == tsum:
				return i + 1
			pre = pre + A[i]
		else:
			return -1


class Solution:

	def equalSum(self, A, N):
		sum_R = 0
		for i in range(N):
			sum_R += A[i]
		sum_L = 0
		for i in range(N):
			sum_R -= A[i]
			if sum_L == sum_R:
				return i + 1
			sum_L += A[i]
		return -1