InfyTQ
Python Coding Question & Answer 2022
Team Division
Problem
Statement -:
Consider an array inarr containing at least two
non-zero positive integers ranging between 1 to 300 (inclusive of the boundary
values). Divide the integers in inarr into two groups based on the below rules:
Each of the integers should belong to either of the
two groups
The total sum of integers in each of the groups must
be as nearly equal as possible
The total number of integers between the two groups
should not differ by more than 1
Print, outnum1 and outnum2, the sum of the integers
of two groups separated by a space. If outnum1 and outnum2 are not equal, then
print the smaller sum followed by the larger sum.
Input
Format:
Read the array inarr elements separated by (‘,’)
comma.
Read the input from the standard input stream.
Output
Format:
Print outnum1 and outnum2 in the required order
separated by a space.
Print the output to the standard output stream.
Sample
Test Cases
Sample
Input
87,100,28,67,68,41,67,1
Sample
Output
229
230
Explanation
For the given input, the two groups that can be
formed following the mentioned rules are:
Group 1: 87 100 41 1
Group 2: 28 67 68 67
The total sum of integers in each of the groups
which is as nearly equal as possible is:
Group 1-Total Sum:229
Group 2-Total Sum:230
The total number of integers between group 1 and 2
differ by O integer.
Program :
a=list(map(int,input("Element of Array :
").split(',')))
n1=len(a)
n2=n1//2
su=sum(a)/2
dp=[[-1 for i in range(n2+1)] for j in range(n1)]
def answer(i,n,s):
if
i<(n-1):
return
float('inf')
if n==0:
return
abs(s)
else:
return
min(answer(i-1,n-1,s-a[i-1]),answer(i-1,n,s))
k=answer(n1-1,n2,su)
print("Group 1 : Total Sum : ",int(su-k))
print("Group 2 : Total sum : ",int(su+k))
Comments
Post a Comment