WeCP Python Coding Question 3 : Profit balance
Problem
Statement-:
Anand and
Brijesh got a bunch of profit in their business. Now it’s time to divide the
profit between themselves. Anand being the one with the calculator was the one
who could decide who will get how much. But as he was one hell of an honest
guy, he can’t cheat on Brijesh to get some extra money. So he decided to divide
them in such a way where they can get almost the same amount possible.
Although, it is sometimes impossible to divide the profit into two, because the
profits must be written in their production managed copy in a way where you
cannot share the profit of a single thing, A single Profit will be held by a
single person.
You are the
one who is called to mitigate the problem. Given an array of profit, find out
in the process, what can be the minimum difference between them in terms of
income.
Constraints:
1<=N<=10^3
0<=Profits<=1000
Input
Format:
First line
contains an integer N, number of profits.
Next line, N
space separated Integers denoting the i-th Profit.
Output:
A single
integer denoting minimum possible difference between the total profits.
Sample
Input:
4
1 2 3 4
Output:
0
Explanation:
He will take
1 and 4, so his profit will be 5, so the difference will be 0.
Program :
# WeCP
Python Coding Question 3 : Profit balance
from
collections import defaultdict
m =
defaultdict(lambda: defaultdict(int))
def
MinDif(i,ss,a,n,m):
global s
if m[i][ss] != 0:
return m[i][ss]
if i==n:
m[i][ss]=abs(s-(2*ss))
return m[i][ss]
if ss>=s//2:
m[i][ss]=abs(s-(2*ss))
return m[i][ss]
m[i][ss]=min(MinDif(i+1,ss+a[i],a,n,m),MinDif(i+1,ss,a,n,m))
return m[i][ss]
n=int(input("Number
of Profit : "))
a=list(map(int,input().split()))
s=sum(a)
print(MinDif(0,0,a,n,m))
For
Output Check the Video :
Comments
Post a Comment