Given an array A of n positive numbers. The task is to find the first Equilibrium Point in an array. 
Equilibrium Point in an array is a position such that the sum of elements before it is equal to the sum of elements after it.
Note: Retun the index of Equilibrium point. (1-based index)
Example 1:
Input: 
n = 5 
A[] = {1,3,5,2,2} 
Output: 3 
Explanation:  
equilibrium point is at position 3 
as 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
its the only equilibrium point.
 
Your Task:
The task is to complete the function equilibriumPoint() which takes the array and n as input parameters and returns the point of equilibrium. Return -1 if no such point exists. 
Expected Time Complexity: O(n)
Expected Auxiliary Space: 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 equilibriumPoint(self,A, N):

        # Your code here



Solutions:
class Solution:

	def equilibriumPoint(self, A, N):
		sumArr = sum(A)
		leftsum = 0
		for (i, number) in enumerate(A):
			sumArr -= number
			if leftsum == sumArr:
				return i + 1
			leftsum += number
		return -1


class Solution:

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


class Solution:

	def equilibriumPoint(self, A, N):
		s = sum(A)
		m = s // 2
		(sum3, sum1, sum2) = (0, 0, 0)
		for i in range(N):
			sum3 += A[i]
			if sum3 >= m:
				ans = i
				break
		for i in range(ans):
			sum1 += A[i]
		for j in range(ans + 1, N):
			sum2 += A[j]
		if sum1 == sum2:
			return ans + 1
		else:
			return -1


class Solution:

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


class Solution:

	def equilibriumPoint(self, A, N):
		sl = 0
		sr = 0
		if N == 1:
			return 1
		for i in range(1, N):
			sr += A[i]
		for i in range(0, N - 1):
			if sr == sl:
				return i + 1
			else:
				sl += A[i]
				sr -= A[i + 1]
		else:
			return -1


class Solution:

	def equilibriumPoint(self, nums, N):
		correct_sum = sum(nums)
		current_sum = 0
		for i in range(len(nums)):
			if current_sum == correct_sum - current_sum - nums[i]:
				return i + 1
			current_sum += nums[i]
		return -1


class Solution:

	def equilibriumPoint(self, nums, N):
		if len(nums) == 1:
			return 1
		left = [nums[0]] * len(nums)
		right = [nums[-1]] * len(nums)
		for i in range(1, len(nums)):
			left[i] = left[i - 1] + nums[i]
			right[len(nums) - i - 1] = right[len(nums) - i] + nums[len(nums) - i - 1]
		if right[1] == 0:
			return 1 + 1
		for i in range(1, len(nums) - 1):
			if left[i - 1] == right[i + 1]:
				return i + 1
		if left[-2] == 0:
			return len(nums) - 1 + 1
		return -1


class Solution:

	def equilibriumPoint(self, A, N):
		rightSum = sum(A)
		leftSum = 0
		for (idx, ele) in enumerate(A):
			rightSum -= ele
			if rightSum == leftSum:
				return idx + 1
			leftSum += ele
		return -1


class Solution:

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


class Solution:

	def equilibriumPoint(self, a, n):
		d = {}
		s = sum(a)
		d[0] = 0
		if len(a) == 1:
			return 1
		for i in range(1, len(a)):
			d[i] = d[i - 1] + a[i - 1]
			if d[i] == (s - a[i]) / 2:
				return i + 1
		return -1


class Solution:

	def equilibriumPoint(self, A, N):
		total = sum(A)
		curr = 0
		for i in range(N):
			if total - curr - A[i] == curr:
				return i + 1
			else:
				curr += A[i]
		return -1


class Solution:

	def equilibriumPoint(self, A, N):
		(postSum, preSum) = (0, 0)
		(left, right) = (0, N - 1)
		while left <= right:
			if postSum == preSum and left == right:
				return left + 1
			if preSum <= postSum:
				preSum += A[left]
				left += 1
			else:
				postSum += A[right]
				right -= 1
		return -1


class Solution:

	def equilibriumPoint(self, A, N):
		(s1, s2) = ([], [])
		for i in range(N):
			if i:
				s1.append(s1[i - 1] + A[i])
				s2.append(s2[i - 1] + A[len(A) - i - 1])
			else:
				s1.append(A[i])
				s2.append(A[len(A) - 1])
		for i in range(N):
			if s1[i] == s2[len(s2) - 1 - i]:
				return i + 1
		return -1


class Solution:

	def equilibriumPoint(self, A, N):
		i = 0
		j = N - 1
		s1 = 0
		s2 = 0
		while i < j:
			if N == 1:
				return 1
			if N == 2:
				return -1
			elif s1 < s2:
				s1 += A[i]
				i += 1
			elif s1 > s2:
				s2 += A[j]
				j -= 1
			else:
				s1 += A[i]
				s2 += A[j]
				i += 1
				j -= 1
		if s1 == s2:
			return i + 1
		return -1


class Solution:

	def equilibriumPoint(self, A, N):
		i = 0
		j = N - 1
		sum1 = 0
		sum2 = 0
		if N == 1:
			return 1
		if N == 2:
			return -1
		while i < j:
			if sum1 > sum2:
				sum2 += A[j]
				j -= 1
			elif sum1 < sum2:
				sum1 += A[i]
				i += 1
			else:
				sum2 += A[j]
				sum1 += A[i]
				i += 1
				j -= 1
		if sum1 == sum2:
			return i + 1
		return -1


class Solution:

	def equilibriumPoint(self, A, N):
		f_sum = 0
		l_sum = 0
		arr1 = []
		arr2 = []
		if N == 1:
			return 1
		for i in A:
			f_sum += i
			arr1.append(f_sum)
		for i in range(N):
			l_sum += A[N - 1 - i]
			arr2.append(l_sum)
		for i in range(1, N):
			if arr1[i] == arr2[N - 1 - i]:
				return i + 1
		return -1


class Solution:

	def equilibriumPoint(self, A, N):
		sum_front = A[0]
		sum_back = A[-1]
		i = 1
		j = N - 1 - i
		if N == 1:
			return 1
		while j >= i:
			if sum_front == sum_back and j == i:
				return i + 1
			if sum_front == sum_back:
				sum_front = sum_front + A[i]
				sum_back = sum_back + A[j]
				i = i + 1
				j = j - 1
			if sum_front < sum_back:
				sum_front = sum_front + A[i]
				i = i + 1
			if sum_front > sum_back:
				sum_back = sum_back + A[j]
				j = j - 1
		return -1


class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

	def equilibriumPoint(self, a, n):
		total = 0
		sum_ = 0
		for i in range(n):
			total += a[i]
		for i in range(n):
			right_sum = total - sum_ - a[i]
			if right_sum == sum_:
				return i + 1
			sum_ += a[i]
		return -1


class Solution:

	def equilibriumPoint(self, A, N):
		(left, right) = (0, sum(A))
		for (i, x) in enumerate(A):
			right -= A[i]
			if left == right:
				return i + 1
			left += A[i]
		return -1


class Solution:

	def equilibriumPoint(self, A, N):
		lsum = 0
		rsum = sum(A)
		for i in range(len(A)):
			lsum += A[i]
			if lsum == rsum:
				return i + 1
			rsum -= A[i]
		return -1


import math

class Solution:

	def equilibriumPoint(self, A, N):
		eq_point = -2
		i = 0
		lsum = 0
		rsum = 0
		for m in range(0, i + 1):
			lsum += A[m]
		for k in range(i + 2, N):
			rsum += A[k]
		if lsum > rsum:
			while lsum > rsum and N > 2:
				lsum -= A[i + 1]
				rsum += A[i + 2]
				i -= 1
		if lsum < rsum:
			while lsum < rsum:
				rsum -= A[i + 2]
				lsum += A[i + 1]
				i += 1
		if lsum == rsum:
			eq_point = i + 1
		if N == 1:
			eq_point = 0
		return eq_point + 1


class Solution:

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


class Solution:

	def equilibriumPoint(self, A, N):
		rsum = 0
		lsum = 0
		for i in range(0, N):
			rsum = rsum + A[i]
		for j in range(0, N):
			rsum = rsum - A[j]
			if rsum == lsum:
				return j + 1
			else:
				lsum = lsum + A[j]
		return -1


class Solution:

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


class Solution:

	def equilibriumPoint(self, A, N):
		x = sum(A)
		curr_sum = 0
		for i in range(N - 1):
			curr_sum += A[i]
			if curr_sum == x - curr_sum - A[i + 1]:
				return i + 2
		if len(A) == 1:
			return 1
		return -1


class Solution:

	def equilibriumPoint(self, A, N):
		total_sum = 0
		for element in A:
			total_sum += element
		curr_sum = 0
		for i in range(len(A)):
			if curr_sum * 2 == total_sum - A[i]:
				return i + 1
			curr_sum += A[i]
		return -1


class Solution:

	def equilibriumPoint(self, A, N):
		left = list()
		right = list()
		sum = 0
		for i in range(N):
			sum += A[i]
			left.append(sum)
		rev_sum = 0
		for j in range(N - 1, -1, -1):
			rev_sum += A[j]
			right.append(rev_sum)
		for i in range(N):
			if left[i] == right[N - 1 - i]:
				return i + 1
		return -1


class Solution:

	def equilibriumPoint(self, arr, n):
		if n != 1 and n != 2:
			a = [0] * n
			a[0] = arr[0]
			b = [0] * n
			b[n - 1] = arr[n - 1]
			for i in range(1, n):
				a[i] = a[i - 1] + arr[i]
			for i in range(n - 2, -1, -1):
				b[i] = b[i + 1] + arr[i]
			a.pop(0)
			a.pop()
			b.pop(0)
			b.pop()
			a = a + b
			r = -1
			for i in range(0, n - 2):
				if a[i] == a[i + (n - 2)]:
					r = i
			if r == -1:
				return -1
			else:
				return r + 2
		elif n == 1:
			return 1
		else:
			return -1


class Solution:

	def equilibriumPoint(self, A, N):
		(currSum, pointSum) = (0, 0)
		currSum = sum(A)
		for i in range(N):
			currSum -= A[i]
			if currSum == pointSum:
				return i + 1
			pointSum += A[i]
		return -1


class Solution:

	def equilibriumPoint(self, arr, N):
		left = 0
		summ = sum(arr)
		cursum = 0
		for i in range(N):
			if summ - arr[i] == left:
				return i + 1
			left += arr[i]
			summ -= arr[i]
		return -1


class Solution:

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


class Solution:

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


class Solution:

	def equilibriumPoint(self, A, N):
		total_sum = sum(A)
		curr_sum = 0
		for (i, val) in enumerate(A):
			if val == total_sum - curr_sum:
				return i + 1
			else:
				curr_sum += val
				total_sum -= val
		return -1


class Solution:

	def equilibriumPoint(self, A, N):
		(left_s, right_s) = (0, 0)
		total_s = sum(A)
		for i in range(N):
			right_s += A[i]
			if left_s == total_s - left_s - A[i]:
				return i + 1
			left_s += A[i]
		return -1


class Solution:

	def equilibriumPoint(self, A, N):
		(left, right, l, r) = (0, 0, 0, N - 1)
		while l != r and l < r:
			if left == right:
				left += A[l]
				right += A[r]
				l += 1
				r -= 1
			elif left > right:
				right += A[r]
				r -= 1
			else:
				left += A[l]
				l += 1
		return l + 1 if left == right and l <= r else -1


class Solution:

	def equilibriumPoint(self, A, N):
		preS = 0
		postS = 0
		l = 0
		r = N - 1
		while l <= r:
			if preS == postS and l == r:
				return l + 1
			if preS <= postS:
				preS += A[l]
				l += 1
			else:
				postS += A[r]
				r -= 1
		return -1


class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

	def equilibriumPoint(self, A, N):
		left_sum = 0
		right_sum = 0
		i = 0
		j = N - 1
		while i < j:
			if i < j:
				left_sum += A[i]
				right_sum += A[j]
				while left_sum < right_sum and i < j:
					i += 1
					left_sum += A[i]
				while right_sum < left_sum and i < j:
					j -= 1
					right_sum += A[j]
				i += 1
				j -= 1
		if left_sum == right_sum and i == j:
			return i + 1
		else:
			return -1


class Solution:

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


class Solution:

	def equilibriumPoint(self, A, n):
		t = sum(A)
		lsum = 0
		if n == 1:
			return 1
		for i in range(n):
			rsum = t - A[i] - lsum
			if lsum == rsum:
				return i + 1
			else:
				lsum = lsum + A[i]
		return -1


class Solution:

	def equilibriumPoint(self, A, N):
		l = list(A)
		ps = [0] * N
		ps[0] = A[0]
		for i in range(1, N):
			ps[i] = ps[i - 1] + A[i]
		psr = [0] * N
		psr[N - 1] = A[N - 1]
		for i in range(N - 2, -1, -1):
			psr[i] = psr[i + 1] + A[i]
		for i in range(N):
			if ps[i] == psr[i]:
				return i + 1
		return -1


class Solution:

	def equilibriumPoint(self, A, N):
		if len(A) == 1:
			return 1
		ans = -1
		l = 0
		r = N
		mid = N // 2
		while l < r - 1:
			if sum(A[:mid]) == sum(A[mid + 1:]):
				return mid + 1
			elif sum(A[:mid]) > sum(A[mid + 1:]):
				r = mid
				mid = (l + r) // 2
			elif sum(A[:mid]) < sum(A[mid + 1:]):
				l = mid
				mid = (l + r) // 2
		return ans


class Solution:

	def equilibriumPoint(self, A, N):
		s = sum(A)
		st = 0
		su = 0
		m = -1
		if N == 1:
			m = 1
		else:
			for i in range(1, N):
				su += A[i - 1]
				sa = s - su - A[i]
				if sa == su:
					m = i + 1
					break
		return m


class Solution:

	def equilibriumPoint(self, A, N):
		s = sum(A)
		i = 0
		while i < N:
			if i > 0:
				A[i] = A[i - 1] + A[i]
			if i == 0:
				lsum = 0
			else:
				lsum = A[i - 1]
			if i == N - 1:
				rsum = 0
			else:
				rsum = s - A[i]
			if lsum == rsum:
				return i + 1
			i += 1
		return -1


class Solution:

	def equilibriumPoint(self, arr, n):

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