Pitsy needs help with the given task by her teacher. The task is to divide an array into two sub-array (left and right) containing n/2 elements each and do the sum of the subarrays and then multiply both the subarrays.
Note: If the length of the array is odd then the right half will contain one element more than the left half.
Example 1:
Input : arr[ ] = {1, 2, 3, 4}
Output : 21
Explanation:
Sum up an array from index 0 to 1 = 3
Sum up an array from index 2 to 3 = 7
Their multiplication is 21.
Example 2:
Input : arr[ ] = {1, 2} 
Output :  2 
 
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 multiply() that takes an array (arr), sizeOfArray (n), and return the sum of the subarrays and then multiply both the subarrays. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ A[i] ≤ 100

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



def multiply (arr, n) : 

    #Complete the function



Solutions:
def multiply(arr, n):
	s1 = 0
	s2 = 0
	x = n // 2
	for i in range(n):
		if i < x:
			s1 += arr[i]
		else:
			s2 += arr[i]
	return s1 * s2


def multiply(arr, n):
	x1 = 0
	x2 = 0
	k = n // 2
	for i in range(n):
		if i < k:
			x1 = x1 + arr[i]
		else:
			x2 = x2 + arr[i]
	return x1 * x2


def multiply(arr, n):
	a = sum(arr[0:n // 2])
	b = sum(arr[n // 2:])
	return a * b


def multiply(arr, n):
	if n % 2 != 0:
		a = sum(arr[0:n // 2])
		b = sum(arr[n // 2:])
	else:
		a = sum(arr[0:n // 2])
		b = sum(arr[n // 2:])
	return a * b


def multiply(arr, n):
	sum1 = 0
	sum2 = 0
	left = -1
	right = -1
	if n % 2 == 0:
		left = int(n / 2)
		right = int(n / 2)
	else:
		left = int((n - 1) / 2)
		right = int((n + 1) / 2)
	for i in range(left):
		sum1 = sum1 + arr[i]
	for j in range(left, n):
		sum2 = sum2 + arr[j]
	return sum1 * sum2


def multiply(arr, n):
	sum1 = 0
	sum2 = 0
	arr1 = arr[0:int(n / 2)]
	arr2 = arr[int(n / 2):]
	for i in range(int(n / 2) + 1):
		try:
			sum2 += arr2[i]
			sum1 += arr1[i]
		except Exception:
			pass
	return sum1 * sum2


def multiply(arr, n):
	a = n // 2
	arr1 = arr[0:a]
	arr2 = arr[a:]
	b = sum(arr1)
	c = sum(arr2)
	return b * c


def multiply(arr, n):
	p1 = 1
	s1 = 0
	p2 = 1
	s2 = 0
	for i in range(n // 2):
		s1 += arr[i]
	for j in range(n // 2, n):
		s2 += arr[j]
	return s1 * s2


def multiply(arr, n):
	if n % 2 == 0:
		sum = 0
		sum1 = 0
		for i in range(int(n / 2)):
			sum = sum + arr[i]
		for j in range(int(n / 2), n):
			sum1 = sum1 + arr[j]
		mul = sum * sum1
		return mul
	else:
		sum2 = 0
		sum3 = 0
		for i in range(int(n // 2)):
			sum2 = sum2 + arr[i]
		for j in range(int(n // 2), n):
			sum3 = sum3 + arr[j]
		mul1 = sum2 * sum3
		return mul1


def multiply(arr, n):
	left = arr[:n // 2]
	right = arr[n // 2:]
	leftsum = 0
	rightsum = 0
	for i in left:
		leftsum += i
	for j in right:
		rightsum += j
	return rightsum * leftsum


def multiply(arr, n):
	left_sum = 0
	right_sum = 0
	middle = int(n / 2)
	right = middle
	left = middle - 1
	while left >= 0:
		left_sum = left_sum + arr[left]
		left = left - 1
	while right < n:
		right_sum = right_sum + arr[right]
		right = right + 1
	return left_sum * right_sum


def multiply(arr, n):
	if n % 2 == 0:
		l1 = arr[:n // 2]
		l2 = arr[n // 2:]
		return sum(l1) * sum(l2)
	else:
		l1 = arr[:n // 2]
		l2 = arr[n // 2:]
		return sum(l1) * sum(l2)


def multiply(arr, n):
	return sum(arr[:n // 2]) * sum(arr[n // 2:])


def multiply(arr, n):
	a1 = arr[0:n // 2]
	a2 = arr[n // 2:n]
	return sum(a1) * sum(a2)


def multiply(arr, n):
	ind = n // 2
	s1 = sum(arr[0:ind])
	s2 = sum(arr[ind:])
	return s1 * s2


def multiply(arr, n):
	a = arr[:n // 2]
	b = arr[n // 2:]
	(sum1, sum2) = (sum(a), sum(b))
	return sum1 * sum2


def multiply(arr, n):
	p = 0
	o = 0
	for i in range(n // 2):
		p = p + arr[i]
	for j in range(n - n // 2):
		o = o + arr[n - 1 - j]
	return p * o


def multiply(arr, n):
	sum = 0
	count = 0
	for i in range(0, n // 2):
		sum = sum + arr[i]
	for j in range(n // 2, n):
		count = count + arr[j]
	return sum * count


def multiply(arr, n):
	lsum = 0
	rsum = 0
	mid = n // 2
	for i in range(0, mid):
		lsum = lsum + arr[i]
	for i in range(mid, n):
		rsum = rsum + arr[i]
	return lsum * rsum


def multiply(arr, n):
	l1 = []
	l2 = []
	for i in range(n // 2):
		l1.append(arr[i])
	for j in arr[n // 2:]:
		l2.append(j)
	a = sum(l1)
	b = sum(l2)
	c = a * b
	return c


def multiply(arr, n):
	sum1 = 0
	sum2 = 0
	for i in range(n // 2):
		sum1 += arr[i]
	for j in range(n // 2, n):
		sum2 += arr[j]
	return sum1 * sum2


def multiply(arr, n):
	(sum1, sum2) = (0, 0)
	end = n // 2
	start = n // 2
	for i in range(end):
		sum1 += arr[i]
	for i in range(start, n):
		sum2 += arr[i]
	return sum1 * sum2


def multiply(arr, n):
	k = n // 2
	l = arr[:k]
	r = arr[k:]
	sl = 0
	sr = 0
	for i in range(k):
		sl += arr[i]
	for i in range(k, n):
		sr += arr[i]
	m = sl * sr
	return m


def multiply(arr, n):
	a = 0
	b = 0
	for i in range(n // 2):
		a += arr[i]
	for i in range(n // 2, n):
		b += arr[i]
	return a * b


def multiply(arr, n):
	m = len(arr) // 2
	a = sum(arr[:m])
	b = sum(arr[m:])
	return a * b


def multiply(arr, n):
	sum1 = 0
	sum2 = 0
	for i in range(n):
		if i < n // 2:
			sum1 += arr[i]
		else:
			sum2 += arr[i]
	return sum1 * sum2


def multiply(arr, n):
	k = sum(arr[:n // 2])
	l = sum(arr[n // 2:])
	return k * l


def multiply(arr, n):
	mid = n // 2
	left = arr[0:mid]
	del arr[0:mid]
	k = sum(left)
	j = sum(arr)
	return j * k


def multiply(arr, n):
	l = list(arr)
	return sum(l[:n // 2]) * sum(l[n // 2:])


def multiply(arr, n):
	k = []
	l = []
	for i in range(n // 2):
		k.append(arr[i])
	for i in range(n // 2, n):
		l.append(arr[i])
	return sum(k) * sum(l)


def multiply(arr, n):
	a = 0
	b = 0
	for x in range(len(arr)):
		if x < len(arr) // 2:
			a += arr[x]
		else:
			b += arr[x]
	return a * b


def multiply(arr, n):
	l = arr[:n // 2]
	r = arr[n // 2:]
	return sum(l) * sum(r)


def multiply(arr, n):
	middle = n // 2
	leftside = arr[:middle]
	rightside = arr[middle:]
	return sum(leftside) * sum(rightside)


def multiply(arr, n):
	t = n // 2
	p1 = 0
	p2 = 0
	if n <= 1:
		return 0
	for i in range(n):
		if i < t:
			p1 += arr[i]
		else:
			p2 += arr[i]
	return p1 * p2


def multiply(arr, n):
	a = []
	b = []
	c = n // 2
	x = 0
	y = 0
	for i in range(c):
		a.append(arr[i])
	for j in range(c, n):
		b.append(arr[j])
	for k in range(len(a)):
		x += a[k]
	for l in range(len(b)):
		y += b[l]
	return x * y


def multiply(arr, n):
	(a1, a2) = ([], [])
	(s1, s2) = (0, 0)
	for i in range(len(arr)):
		a1 = arr[:int(len(arr) / 2)]
		a2 = arr[int(len(arr) / 2):]
		s1 = sum(a1)
		s2 = sum(a2)
	return s1 * s2


def multiply(arr, n):
	k = n // 2
	l = arr[:k]
	r = arr[k:]
	add1 = 0
	add2 = 0
	for i in range(k):
		add1 += arr[i]
	for j in range(k, n):
		add2 += arr[j]
	return add1 * add2


def multiply(arr, n):
	new = arr[:n // 2]
	sub = arr[n // 2:]
	ans = sum(new) * sum(sub)
	return ans


def multiply(arr, n):
	a = arr[:n // 2]
	b = arr[n // 2:]
	sum1 = 0
	sum2 = 0
	for i in a:
		sum1 += i
	for i in b:
		sum2 += i
	return sum1 * sum2


def multiply(arr, n):
	s1 = 0
	s2 = 0
	for i in arr[:int(n / 2)]:
		s1 += i
	for i in arr[int(n / 2):]:
		s2 += i
	return s1 * s2


def multiply(arr, n):
	mid = n // 2
	left = arr[:mid]
	right = arr[mid:]
	return sum(left) * sum(right)


def multiply(arr, n):
	a = len(arr) // 2
	c = sum(arr[a:])
	d = sum(arr[:a])
	return c * d


def multiply(arr, n):
	s1 = 0
	s2 = sum(arr)
	for i in range(n // 2):
		s1 += arr[i]
	x = s2 - s1
	return x * s1


def multiply(arr, n):
	a = n // 2
	s = sum(arr[:a])
	s1 = sum(arr[a:n])
	return s * s1


def multiply(arr, n):
	c = n // 2
	lm = 0
	rm = 0
	for i in range(0, c):
		lm += arr[i]
	for j in range(c, n):
		rm += arr[j]
	return lm * rm


def multiply(arr, n):
	sm = sum(arr[:n // 2])
	sn = sum(arr[n // 2:])
	r = sm * sn
	return r


def multiply(arr, n):
	sm = 0
	sn = 0
	for i in range(0, n // 2):
		sm = sm + arr[i]
	for j in range(n // 2, n):
		sn = sn + arr[j]
	ans = sm * sn
	return ans


def multiply(arr, n):
	l = n // 2
	sum1 = 0
	sum2 = 0
	ans = 0
	for i in range(0, l):
		sum1 += arr[i]
	for i in range(l, n):
		sum2 += arr[i]
	ans = sum1 * sum2
	return ans


def multiply(arr, n):
	p = int(n / 2)
	sum1 = 0
	sum2 = 0
	for i in range(p):
		sum1 = sum1 + arr[i]
	for j in range(p, n):
		sum2 = sum2 + arr[j]
	c = sum1 * sum2
	return c


def multiply(arr, n):
	l = n // 2
	x = sum(arr[:l])
	y = sum(arr[l:])
	return x * y


def multiply(arr, n):
	Sum = 0
	for i in range(0, int(n / 2)):
		Sum += arr[i]
		i += 1
	a = 0
	for i in range(int(n / 2), n):
		a += arr[i]
		i += 1
	c = Sum * a
	return c


def multiply(arr, n):
	mid = n // 2
	a = arr[0:mid]
	b = arr[mid:n + 1]
	sum1 = 0
	sum2 = 0
	for i in a:
		sum1 += i
	for j in b:
		sum2 += j
	return sum1 * sum2


def multiply(arr, n):
	l = []
	r = []
	if n % 2 == 0:
		for i in range(0, n):
			if i < n / 2:
				l.append(arr[i])
			else:
				r.append(arr[i])
		return sum(l) * sum(r)
	else:
		x = int(n / 2)
		for i in range(0, n):
			if i < x:
				l.append(arr[i])
			else:
				r.append(arr[i])
		return sum(l) * sum(r)


def multiply(arr, n):
	l = len(arr)
	(sum1, sum2) = (0, 0)
	if l % 2 == 0:
		for i in range(0, l // 2):
			sum1 += arr[i]
		for j in range(l // 2, l):
			sum2 += arr[j]
		return sum1 * sum2
	else:
		for i in range(0, l // 2):
			sum1 += arr[i]
		for j in range(l // 2, l):
			sum2 += arr[j]
		return sum1 * sum2


def multiply(arr, n):
	k = n // 2
	left = arr[:k]
	right = arr[k:]
	add1 = 0
	add2 = 0
	for i in range(k):
		add1 += arr[i]
	for j in range(k, n):
		add2 += arr[j]
	return add1 * add2


def multiply(arr, n):
	k = n // 2
	left = arr[:k]
	right = arr[k:]
	return sum(left) * sum(right)


def multiply(arr, n):
	import math
	if n == 1:
		return 0
	if n == 2:
		p = 1
		for i in arr:
			p *= i
		return p
	else:
		return sum(arr[:n // 2]) * sum(arr[n // 2:])


def multiply(arr, n):
	s1 = s2 = 0
	for i in range(n // 2):
		s1 += arr[i]
	for j in range(n // 2, n):
		s2 += arr[j]
	return s1 * s2


def multiply(arr, n):
	arr1 = arr[:n // 2]
	arr2 = arr[n // 2:]
	sum1 = sum(arr1)
	sum2 = sum(arr2)
	return sum1 * sum2


def multiply(arr, n):
	if n % 2 == 0:
		s1 = 0
		for i in range(int(n // 2)):
			s1 = s1 + arr[i]
		s2 = 0
		for i in range(int(n // 2), n):
			s2 = s2 + arr[i]
		return s1 * s2
	else:
		s1 = 0
		for i in range(int((n - 1) // 2)):
			s1 = s1 + arr[i]
		s2 = 0
		for i in range(int((n - 1) // 2), n):
			s2 = s2 + arr[i]
		return s1 * s2


def multiply(arr, n):
	num = n // 2
	arr1 = arr[:num]
	arr2 = arr[num:]
	return sum(arr1) * sum(arr2)


def multiply(arr, n):
	lo = 0
	up = n
	a = 0
	b = 0
	mid = (lo + up) // 2
	for i in range(lo, mid):
		a += arr[i]
	for j in range(mid, up):
		b += arr[j]
	c = a * b
	return c


def multiply(arr, n):
	sum1 = 0
	sum2 = 0
	sum3 = 0
	sum4 = 0
	if n % 2 == 0 or n % 2 != 0:
		for i in range(0, n // 2):
			sum1 = sum1 + arr[i]
		for i in range(n // 2, n):
			sum2 = sum2 + arr[i]
		return sum1 * sum2


def multiply(arr, n):
	mid = n // 2
	t1 = 0
	t2 = 0
	for i in range(n):
		if i < mid:
			t1 += arr[i]
		else:
			t2 += arr[i]
	return t1 * t2


def multiply(arr, n):
	z = []
	f = []
	if n % 2 == 0:
		for i in range(int(n / 2)):
			z.append(arr[i])
		for i in range(int(n / 2), n):
			f.append(arr[i])
		return sum(z) * sum(f)
	else:
		for i in range(int(n / 2)):
			z.append(arr[i])
		for i in range(int(n / 2), n):
			f.append(arr[i])
		return sum(z) * sum(f)


def multiply(arr, n):
	p = sum(arr[0:n // 2])
	q = sum(arr[n // 2:])
	return p * q