Given an array of even size N, task is to find minimum value that can be added to an element so that array become balanced. An array is balanced if the sum of the left half of the array elements is equal to the sum of right half. 
Example 1:
Input:
N = 4
arr[] = {1, 5, 3, 2}
Output: 1
Explanation: 
Sum of first 2 elements is 1 + 5  = 6, 
Sum of last 2 elements is 3 + 2  = 5,
To make the array balanced you can add 1.
Example 2:
Input:
N = 6
arr[] = { 1, 2, 1, 2, 1, 3 }
Output: 2
Explanation:
Sum of first 3 elements is 1 + 2 + 1 = 4, 
Sum of last three elements is 2 + 1 + 3 = 6,
To make the array balanced you can add 2.
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function minValueToBalance() which takes the array a[] and N as inputs and returns the desired result.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
2<=N<=10^{7}
N%2==0

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



class Solution:

    def minValueToBalance(self,a,n):

        #code here.

Solutions:
class Solution:

	def minValueToBalance(self, a, n):
		mid = n // 2
		sum1 = 0
		sum2 = 0
		diff = 0
		for i in range(mid):
			sum1 += a[i]
		for i in range(mid, n):
			sum2 += a[i]
		diff = abs(sum1 - sum2)
		return diff


class Solution:

	def minValueToBalance(self, a, n):
		e = sum(a)
		li = []
		li1 = []
		for i in range(0, n // 2):
			li.append(a[i])
		for j in range(n // 2, n):
			li1.append(a[j])
		c = sum(li)
		d = sum(li1)
		return abs(c - d)


class Solution:

	def minValueToBalance(self, a, n):
		left_sum = 0
		right_sum = 0
		even = False
		if n % 2 == 0:
			even = True
		left_arr = a[0:int(n / 2)]
		right_arr = a[int(n / 2):]
		if not even:
			right_arr.pop(0)
		for i in range(int(n / 2)):
			left_sum += left_arr[i]
			right_sum += right_arr[i]
		return abs(right_sum - left_sum)


class Solution:

	def minValueToBalance(self, a, n):
		if n % 2 == 0:
			sum = 0
			sum1 = 0
			for i in range(int(n / 2)):
				sum = sum + a[i]
			for j in range(int(n / 2), n):
				sum1 = sum1 + a[j]
			diff = abs(sum - sum1)
			return diff
		else:
			sum2 = 0
			sum3 = 0
			for i in range(int(n // 2)):
				sum2 = sum2 + a[i]
			for j in range(int(n // 2), n):
				sum3 = sum3 + a[j]
			diff = abs(sum2 - sum3)
			return diff


class Solution:

	def minValueToBalance(self, a, n):
		l = []
		l1 = []
		k = len(a) // 2
		for i in range(0, k):
			l.append(a[i])
		for i in range(k, n):
			l1.append(a[i])
		s = sum(l)
		s1 = sum(l1)
		return abs(s - s1)


class Solution:

	def minValueToBalance(self, a, n):
		left = a[:n // 2]
		right = a[n // 2:]
		return abs(sum(left) - sum(right))


class Solution:

	def minValueToBalance(self, a, n):
		mid = len(a) // 2
		left_half = a[:mid]
		right_half = a[mid:]
		return abs(sum(left_half) - sum(right_half))


class Solution:

	def minValueToBalance(self, a, n):
		ind = n // 2
		sum1 = sum(a[:ind])
		sum2 = sum(a[ind:])
		return abs(sum1 - sum2)


class Solution:

	def minValueToBalance(self, a, n):
		t = len(a) // 2
		s1 = sum(a[0:t])
		s2 = sum(a[t:])
		return abs(s1 - s2)


class Solution:

	def minValueToBalance(self, a, n):
		b = a[:n // 2]
		c = a[n // 2:]
		(sum1, sum2) = (0, 0)
		for i in range(n // 2):
			sum1 += b[i]
			sum2 += c[i]
		return abs(sum2 - sum1)


class Solution:

	def minValueToBalance(self, a, n):
		return abs(sum(a[:int(n / 2)]) - sum(a[int(n / 2):]))


class Solution:

	def minValueToBalance(self, a, n):
		c = 0
		d = 0
		mid = n // 2
		for i in range(0, mid):
			c = c + a[i]
		for i in range(mid, n):
			d = d + a[i]
		e = abs(c - d)
		return e


class Solution:

	def minValueToBalance(self, a, n):
		n = len(a) // 2
		fs = sum(a[:n])
		ls = sum(a[n:])
		return abs(fs - ls)


class Solution:

	def minValueToBalance(self, a, n):
		middle = n // 2
		return abs(sum(a[:middle]) - sum(a[middle:]))


class Solution:

	def minValueToBalance(self, arr, n):
		n1 = n // 2
		a1 = list(arr[:n1])
		a2 = list(arr[n1:])
		d = abs(sum(a1) - sum(a2))
		return d


class Solution:

	def minValueToBalance(self, a, n):
		mid = n // 2
		s1 = a[0:mid]
		s2 = a[mid:n]
		return abs(sum(s1) - sum(s2))


class Solution:

	def minValueToBalance(self, a, n):
		mid = n // 2
		s1 = 0
		s2 = 0
		for i in range(0, mid):
			s1 = s1 + a[i]
		for i in range(mid, n):
			s2 = s2 + a[i]
		return abs(s1 - s2)
		diff = a[avg:] - a[:avg]


class Solution:

	def minValueToBalance(self, a, n):
		l1 = []
		l2 = []
		for i in range(n // 2):
			l1.append(a[i])
		for j in a[n // 2:]:
			l2.append(j)
		a = sum(l1)
		b = sum(l2)
		c = abs(a - b)
		return c


class Solution:

	def minValueToBalance(self, a, n):
		k = n // 2
		l = a[0:k]
		l1 = a[k:n]
		return abs(sum(l) - sum(l1))


class Solution:

	def minValueToBalance(self, a, n):
		mid = n // 2
		left_side = a[:mid]
		right_side = a[mid:]
		return abs(sum(left_side) - sum(right_side))


class Solution:

	def minValueToBalance(self, a, n):
		m = n // 2
		s1 = 0
		s2 = 0
		for i in range(0, m):
			s1 += a[i]
		for j in range(m, n):
			s2 += a[j]
		if s1 > s2:
			return s1 - s2
		elif s2 >= s1:
			return s2 - s1


class Solution:

	def minValueToBalance(self, a, n):
		b = sum(a[:n // 2])
		c = sum(a[n // 2:])
		return abs(b - c)


class Solution:

	def minValueToBalance(self, a, n):
		k = n // 2
		left = a[0:k]
		del a[0:k]
		right = a
		k = sum(left) - sum(right)
		return abs(k)


class Solution:

	def minValueToBalance(self, a, n):
		a1 = sum(a[:n // 2])
		a2 = sum(a[n // 2:])
		return abs(a1 - a2)


class Solution:

	def minValueToBalance(self, arr, n):
		(s1, s2) = (0, 0)
		for i in range(0, len(arr) // 2):
			s1 += arr[i]
		for j in range(len(arr) // 2, len(arr)):
			s2 += arr[j]
		if s1 > s2:
			return s1 - s2
		else:
			return s2 - s1


class Solution:

	def minValueToBalance(self, a, n):
		first = 0
		second = 0
		for i in range(int(n / 2)):
			first += a[i]
		for j in range(int(n / 2), int(n)):
			second += a[j]
		if second > first:
			result = second - first
		else:
			result = first - second
		return result


class Solution:

	def minValueToBalance(self, a, n):
		leftsum = sum(a[:n // 2])
		rightsum = sum(a[n // 2:])
		difference = abs(rightsum - leftsum)
		return difference


class Solution:

	def minValueToBalance(self, a, n):
		half = n // 2
		lefthalf = a[:half:1]
		righthalf = a[half::1]
		j = sum(lefthalf)
		k = sum(righthalf)
		d = min(j, k)
		cnt = 0
		if j < k:
			for i in range(1000000000000):
				if j != k:
					j = j + 1
					cnt = cnt + 1
				else:
					return cnt
		else:
			for i in range(100000000000):
				if j != k:
					k = k + 1
					cnt = cnt + 1
				else:
					return cnt


class Solution:

	def minValueToBalance(self, a, n):
		l = sum(a[:n // 2])
		r = sum(a[n // 2:])
		return abs(l - r)


class Solution:

	def minValueToBalance(self, a, n):
		halfsum = sum(a) // 2
		leftsum = sum(a[:n // 2])
		rightsum = sum(a[n // 2:])
		diff = abs(leftsum - rightsum)
		return diff


class Solution:

	def minValueToBalance(self, a, n):
		p1 = 0
		p2 = 0
		t = n // 2
		for i in range(n):
			if i < t:
				p1 += a[i]
			else:
				p2 += a[i]
		return abs(p1 - p2)


class Solution:

	def minValueToBalance(self, a, n):
		l1 = a[0:n // 2]
		l2 = a[n // 2:]
		s1 = sum(l1)
		s2 = sum(l2)
		if s1 > s2:
			return s1 - s2
		else:
			return s2 - s1


class Solution:

	def minValueToBalance(self, a, n):
		x = []
		y = []
		c = 0
		d = 0
		for i in range(n // 2):
			x.append(a[i])
		for j in range(n // 2, n):
			y.append(a[j])
		for k in range(len(x)):
			c += x[k]
		for l in range(len(y)):
			d += y[l]
		if c > d:
			return c - d
		else:
			return d - c


class Solution:

	def minValueToBalance(self, a, n):
		b = n // 2
		s1 = sum(a[:b])
		s2 = sum(a[b:])
		return abs(s1 - s2)


class Solution:

	def minValueToBalance(self, a, n):
		e = int(n / 2)
		q = int(n / 2)
		g = sum(a[:e])
		f = sum(a[e:])
		return abs(g - f)


import math

class Solution:

	def minValueToBalance(self, a, n):
		right = 0
		left = 0
		for i in a[:n // 2]:
			left += i
		for i in a[n // 2:]:
			right += i
		if right > left:
			return right - left
		return left - right


class Solution:

	def minValueToBalance(self, a, n):
		suml = 0
		sumr = 0
		for i in range(0, n // 2):
			suml += a[i]
		for i in range(n // 2, n):
			sumr += a[i]
		diff = sumr - suml
		if diff < 0:
			return -1 * diff
		return diff


class Solution:

	def minValueToBalance(self, arr, n):
		sn = sum(arr[:n // 2])
		sm = sum(arr[n // 2:])
		return abs(sn - sm)


class Solution:

	def minValueToBalance(self, a, n):
		sm = 0
		sn = 0
		for i in range(0, n // 2):
			sm = sm + a[i]
		for i in range(n // 2, n):
			sn = sn + a[i]
		return abs(sm - sn)


class Solution:

	def minValueToBalance(self, a, n):
		leftsum = 0
		rightsum = 0
		for i in range(n // 2):
			leftsum += a[i]
		for i in range(n // 2, n):
			rightsum += a[i]
		return abs(leftsum - rightsum)


class Solution:

	def minValueToBalance(self, a, n):
		sl = 0
		sr = 0
		for i in range(n // 2):
			sl += a[i]
		for i in range(n // 2, n):
			sr += a[i]
		if sl > sr:
			return sl - sr
		else:
			return sr - sl


class Solution:

	def minValueToBalance(self, a, n):
		x = a[:n // 2]
		y = a[n // 2:]
		r = sum(x)
		s = sum(y)
		i = 0
		if r > s:
			i = r - s
		else:
			i = s - r
		return i


class Solution:

	def minValueToBalance(self, a, n):
		sum1 = 0
		for i in range(int(n / 2)):
			sum1 += a[i]
		sum2 = 0
		i = int(n / 2)
		while i < n:
			sum2 += a[i]
			i = i + 1
		return abs(sum1 - sum2)


class Solution:

	def minValueToBalance(self, a, n):
		l = r = 0
		for i in range(0, n):
			if i < n // 2:
				l = l + a[i]
			else:
				r = r + a[i]
		return abs(l - r)


class Solution:

	def minValueToBalance(self, a, n):
		leftsum = 0
		for i in range(0, n // 2):
			leftsum = leftsum + a[i]
		rightsum = 0
		for i in range(n // 2, n):
			rightsum = rightsum + a[i]
		ans = 0
		if leftsum > rightsum:
			ans = leftsum - rightsum
		else:
			ans = rightsum - leftsum
		return ans


class Solution:

	def minValueToBalance(self, a, n):
		sum = 0
		s = 0
		for i in range(0, n // 2):
			sum = sum + a[i]
		for i in range(n // 2, len(a)):
			s = s + a[i]
		if sum > s:
			return sum - s
		elif s > sum:
			return s - sum


class Solution:

	def minValueToBalance(self, arr, n):
		a = 0
		b = 0
		for i in range(0, n // 2):
			a = a + arr[i]
		for i in range(n // 2, n):
			b += arr[i]
		return abs(b - a)


class Solution:

	def minValueToBalance(self, a, n):
		c = 0
		d = 0
		for i in range(int(n / 2)):
			c += a[i]
		for i in range(int(n / 2), n):
			d += a[i]
		if d >= c:
			b = d - c
		else:
			b = c - d
		return b


class Solution:

	def minValueToBalance(self, a, n):
		(left, right) = (0, n - 1)
		(left_sum, right_sum) = (0, 0)
		while left < right:
			left_sum += a[left]
			right_sum += a[right]
			left += 1
			right -= 1
		return abs(left_sum - right_sum)


class Solution:

	def minValueToBalance(self, a, n):
		k = n // 2
		FIRST_LIST = a[:k]
		SECOND_LIST = a[k:]
		k = abs(sum(FIRST_LIST) - sum(SECOND_LIST))
		return k


class Solution:

	def minValueToBalance(self, a, n):
		s = sum(a)
		x = 0
		for i in range(n // 2):
			x = x + a[i]
		return abs(x - (s - x))


class Solution:

	def minValueToBalance(self, a, n):
		if n % 2 == 0:
			s1 = 0
			for i in range(int(n // 2)):
				s1 = s1 + a[i]
			s2 = 0
			for i in range(int(n // 2), n):
				s2 = s2 + a[i]
			return abs(s2 - s1)
		else:
			s1 = 0
			for i in range(int((n - 1) // 2)):
				s1 = s1 + a[i]
			s2 = 0
			for i in range(int((n - 1) // 2), n):
				s2 = s2 + a[i]
			return abs(s2 - s1)


class Solution:

	def minValueToBalance(self, a, n):
		s = n // 2
		c = 0
		h = 0
		for i in range(s):
			c += a[i]
		for j in range(s, n):
			h += a[j]
		return abs(c - h)


class Solution:

	def minValueToBalance(self, a, n):
		x = 0
		y = 0
		mid = n // 2
		for i in range(mid):
			x += a[i]
		for j in range(mid, n):
			y += a[j]
		return abs(x - y)


class Solution:

	def minValueToBalance(self, a, n):
		sum = 0
		cum = 0
		for i in range(len(a) // 2):
			sum += a[i]
		for i in range(n // 2, len(a)):
			cum += a[i]
		d = sum - cum
		return abs(d)


class Solution:

	def minValueToBalance(self, a, n):
		mid = n // 2
		a1 = a[:mid]
		a2 = a[mid:]
		if sum(a1) - sum(a2) > 0:
			return sum(a1) - sum(a2)
		else:
			return sum(a2) - sum(a1)


class Solution:

	def minValueToBalance(self, a, n):
		mid = n // 2
		left = a[:mid]
		ryht = a[mid:]
		if sum(left) < sum(ryht):
			return sum(ryht) - sum(left)
		else:
			return sum(left) - sum(ryht)


class Solution:

	def minValueToBalance(self, a, n):
		s1 = s2 = 0
		for i in range(n // 2):
			s1 += a[i]
		for j in range(n // 2, n):
			s2 += a[j]
		return abs(s1 - s2)


class Solution:

	def minValueToBalance(self, a, n):
		mid = n // 2
		arr1 = sum(a[:mid])
		arr2 = sum(a[mid:])
		return abs(arr1 - arr2)


class Solution:

	def minValueToBalance(self, a, n):
		sum1 = 0
		sum2 = 0
		for i in range(0, n // 2):
			sum1 = sum1 + a[i]
		for i in range(n // 2, n):
			sum2 = sum2 + a[i]
		if sum1 == sum2:
			return 0
		else:
			return abs(sum1 - sum2)


class Solution:

	def minValueToBalance(self, a, n):
		n = len(a) // 2
		c = sum(a[:n])
		b = sum(a[n:])
		if c > b:
			return c - b
		else:
			return b - c


class Solution:

	def minValueToBalance(self, a, n):
		n = len(a) // 2
		a1 = sum(a[0:n])
		b1 = sum(a[n:])
		if a1 > b1:
			return a1 - b1
		else:
			return b1 - a1


class Solution:

	def minValueToBalance(self, arr, n):
		list1 = list(arr)
		len1 = len(list1)
		len1a = int(len1 / 2)
		len1b = list(arr[len1a:])
		sum1 = 0
		sum2 = 0
		for i in list1[0:len1a]:
			sum1 = sum1 + i
		for j in list1[len1a:]:
			sum2 = sum2 + j
		return abs(sum1 - sum2)


class Solution:

	def minValueToBalance(self, a, n):
		h = int(len(a) / 2)
		s1 = 0
		for i in range(0, h):
			s1 = s1 + a[i]
		s2 = 0
		for i in range(h, len(a)):
			s2 = s2 + a[i]
		return int(abs(s2 - s1))


class Solution:

	def minValueToBalance(self, a, n):
		p = a[0:n // 2]
		q = a[n // 2:]
		r = sum(p)
		s = sum(q)
		return abs(r - s)


class Solution:

	def minValueToBalance(self, a, n):
		s1 = 0
		s = 0
		if len(a) % 2 == 0:
			for i in range(0, len(a) // 2):
				s += a[i]
			for j in range(len(a) // 2, len(a)):
				s1 += a[j]
		diff = abs(s - s1)
		if s < s1:
			s += diff
		else:
			s1 += diff
		return diff