Given an array S consisting of N numbers, find the sum of difference between last and first element of each subset.
Example 1:
Input:
S = [5,8]
Output: 
3
Explanation: There are 3 subsets possible for the given array S.
1 -> [5] having first and last element same i.e. 5
so the difference is 0.
2 -> [8] having first and last element same i.e. 8
so the difference is 0.
3 -> [5,8] having first and last element as 5 and 8
respectively. So the difference is 8-5 = 3
Total difference is 0 + 0 + 3 = 3
Your Task:
You don't need to read input or print anything. You are required to complete the function sumDiff which returns an integer denoting the sum of difference between last and first element for each subset of array S.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1<=T<=100
1<=N<=20
1<=S[]<=1000

Difficulty: EASY
Tags: Data structures
Skill types: Data structures
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Starter code:
#Your task is to complete this function

#Your function should return desired Answer

def sumDiff(S, n):

    #Code here

Solutions:
from math import factorial

def sumDiff(S, n):
	output = 0
	for (i, item) in enumerate(S):
		output += item * 2 ** i
		output -= item * 2 ** (n - i - 1)
	return output


def sumDiff(S, n):
	sumF = 0
	sumL = 0
	for i in range(n):
		sumF += S[i] * 2 ** (n - i - 1)
		sumL += S[i] * 2 ** i
	return sumL - sumF


def sumDiff(S, n):
	l = 0
	for i in range(n):
		l += S[i] * pow(2, n - i - 1)
	r = 0
	for i in range(n):
		r += S[i] * pow(2, i)
	return r - l


def sumf(S, n):
	summ = 0
	for i in range(n):
		summ += S[i] * pow(2, n - i - 1)
	return summ

def suml(S, n):
	summ = 0
	for i in range(n):
		summ += S[i] * pow(2, i)
	return summ

def sumDiff(S, n):
	return suml(S, n) - sumf(S, n)


def sumDiff(S, n):
	tot = index = 0
	while index < n:
		rindex = index + 1
		while rindex < n:
			tot += 2 ** (rindex - index - 1) * (S[rindex] - S[index])
			rindex += 1
		index += 1
	return tot


def sumDiff(S, n):
	ans = 0
	for i in range(n - 1, 0, -1):
		for j in range(0, i):
			ans += (S[i] - S[j]) * pow(2, i - j - 1)
	return ans


def sumDiff(S, n):
	summ = 0
	for i in range(n):
		for j in range(i, n):
			summ += (S[j] - S[i]) * 2 ** (j - i - 1)
	k = int(summ)
	return k


def sumDiff(S, n):
	sum_first = 0
	for i in range(n):
		sum_first += S[i] * pow(2, n - i - 1)
	sum_last = 0
	for i in range(n):
		sum_last += S[i] * pow(2, i)
	return sum_last - sum_first


def sumDiff(S, n):
	lans = 0
	for i in range(n):
		lans += S[i] * pow(2, n - i - 1)
	rans = 0
	for i in range(n):
		rans += S[i] * pow(2, i)
	return rans - lans


def sumDiff(S, n):
	total = 0
	for i in range(n):
		sum_i = 2 ** i * S[i]
		sum_i = sum_i - 2 ** (n - i - 1) * S[i]
		total = total + sum_i
	return total


def sumDiff(S, n):
	ans = 0
	for first in range(n - 1):
		for last in range(first + 1, n):
			ans += (arr[last] - arr[first]) * 2 ** (last - first - 1)
	return ans


def sumDiff(S, n):
	left = 0
	for i in range(n):
		left += S[i] * pow(2, n - i - 1)
	right = 0
	for i in range(n):
		right += S[i] * pow(2, i)
	return right - left


def sumL(s, n):
	sum_ = 0
	for i in range(n):
		sum_ = sum_ + s[i] * pow(2, i)
	return sum_

def sumF(s, n):
	sum_ = 0
	for i in range(n):
		sum_ = sum_ + s[i] * pow(2, n - i - 1)
	return sum_

def sumDiff(S, n):
	return sumL(S, n) - sumF(S, n)


def sumDiff(S, n):
	s = 0
	for i in range(0, len(S) - 1):
		for j in range(i, len(S)):
			s += (S[j] - S[i]) * 2 ** (j - i - 1)
	return int(s)


def sumDiff(S, n):

	def SumF(S, n):
		sum = 0
		for i in range(n):
			sum = sum + S[i] * pow(2, n - i - 1)
		return sum

	def SumL(S, n):
		sum = 0
		for i in range(n):
			sum = sum + S[i] * pow(2, i)
		return sum
	return SumL(S, n) - SumF(S, n)


def sumDiff(S, n):
	n = n - 1
	maxi = mini = 0
	for i in range(n + 1):
		maxi = maxi + S[i] * (1 << i)
		mini = mini + S[i] * (1 << n - i)
	return maxi - mini


def sumDiff(a, n):
	sum = 0
	i = 0
	while i < n:
		j = i + 1
		while j < n:
			sum = sum + pow(2, j - i - 1) * (a[j] - a[i])
			j += 1
		i += 1
	return sum


def sumDiff(S, n):
	import math
	sum = 0
	for i in range(n - 1, 0, -1):
		for j in range(0, i):
			sum += (S[i] - S[j]) * int(math.pow(2, i - j - 1))
	return sum


def sumDiff(S, n):
	diff_sum = 0
	for i in range(n - 1, -1, -1):
		last_ele = S[i]
		for j in range(0, i):
			first_ele = S[j]
			diff_sum += (last_ele - first_ele) * 2 ** (i - j - 1)
	return diff_sum


def sumDiff(S, n):
	ans = 0
	(i, j) = (0, n - 1)
	while i < n and j >= 0:
		ans += S[j] * (1 << j)
		ans -= S[i] * (1 << j)
		i += 1
		j -= 1
	return ans


def sumDiff(arr, n):
	sumF = 0
	sumL = 0
	for i in range(n):
		sumF += arr[i] * pow(2, n - i - 1)
	for i in range(n):
		sumL += arr[i] * pow(2, i)
	result = sumL - sumF
	return result