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:

  1. Each of the integers should belong to either of the two groups
  2. The total sum of integers in each of the groups must be as nearly equal as possible
  3. 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):


nIntegerThe number of rows in the map.
mIntegerThe number of columns in the map.
xIntegerThe blocked cell’s row.
yIntegerThe blocked cell’s column.


  • 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
  • Sample Output
    229 230


For the given input, the two groups that can be formed following the mentioned rules are:

  1. Group 1: 87 100 41 1
  2. Group 2: 28 67 68 67

The total sum of integers in each of the groups which is as nearly equal as possible is:

  1. Group 1-Total Sum:229
  2. 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;
int c=0;
for(int i=0;i<s.length();i++)
if(s[i]==',') {v.push_back(stoi(s.substr(c,i-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;

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)
return min(answer(i-1,n-1,s-a[i-1]),answer(i-1,n,s))