Question 9: Team Division
Team Division
Today we will discuss InfyTQ Coding question that is Team Division that was asked in InfyTQ Coding question InfyTQ Advance Coding Section. We will see the Question description along with function description and the Test Cases along with there explanation.
You will find the solution of the problem which is asked for InfyTQ Coding question in various programming language.
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.
Function Description:
- Complete the markgame function in the editor below. It has the following parameter(s):
Parameters:
Name | Type | Description |
---|---|---|
n | Integer | The number of rows in the map. |
m | Integer | The number of columns in the map. |
x | Integer | The blocked cell’s row. |
y | Integer | The blocked cell’s column. |
Constraints:
- 1 <= n <= 10^2
- 1 <= m <= 10^2
- 1 <= x <= n
- 1 <= y <= m
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.
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int ans(int i,int n,float s)
{
if(i<n-1) return INT_MAX;
if(n==0) return abs(s);
return min(ans(i-1,n-1,s-v[i-1]),ans(i-1,n,s));
}
int main()
{
string s;
cin>>s;
int c=0;
for(int i=0;i<s.length();i++)
{
if(s[i]==',') {v.push_back(stoi(s.substr(c,i-c)));
c=i+1;}
}
v.push_back(stoi(s.substr(c,s.length()-c)));
//for(int i=0;i<v.size();i++) cout<<v[i];
int ss=0;
for(int i=0;i<v.size();i++) ss+=v[i];
int n1=v.size();
int n2=n1/2;
float su=ss/2;
int k=ans(n1-1,n2,su);
cout<<ss-su-k<<" "<<su+k;
}
a=list(map(int,input().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(int(su-k),int(su+k))
Login/Signup to comment