Organizing Containers of Balls

Ashish Patel
Codebrace
Published in
3 min readFeb 21, 2017

Problem Link: Organizing Containers of Balls

David has n containers and n different types of balls, both of which are numbered from 0 to n-1. The distribution of ball types per container are described by an n x n matrix of integers, M, where each Mc,t is the number of balls of type t in container c. For example, consider the following diagram for M= [[1,4],[2,3]]:

image

In a single operation, David can swap two balls located in different containers (i.e., one ball is moved from container ca to cb and the other ball is moved from cb to ca).

For example, the diagram below depicts a single swap operation:

image

David wants to perform some number of swap operations such that both of the following conditions are satisfied:

  • Each container contains only balls of the same type.
  • No two balls of the same type are located in different containers.

You must perform q queries where each query is in the form of a matrix, M. For each query, print Possible on a new line if David can satisfy the conditions above for the given matrix; otherwise, print Impossible instead.

Input Format

The first line contains an integer denoting q (the number of queries). The subsequent lines describe each query in the following format:

  1. The first line contains an integer denoting n (the number of containers and ball types).
  2. Each line i of the subsequent lines contains n space-separated integers describing row in matrix M.

Constraints

1 <= q <= 10
1 <= n <= 100
1 <= Mc,t <= 109

Scoring

  • For 33% of score, 1 <= n <= 10.
  • For 100% of score, 1 <= n <= 100.

Output Format

For each query, print Possible on a new line if David can satisfy the conditions above for the given matrix; otherwise, print Impossible instead.

Sample Input 0

2
2
1 1
1 1
2
0 2
1 1

Sample Output 0

Possible
Impossible

Explanation 0

We perform the following q=2 queries:

  1. The diagram below depicts one possible way to satisfy David’s requirements for the first query:
image
  1. Thus, we print Possible on a new line.
  2. The diagram below depicts the matrix for the second query:
image
  1. No matter how many times we swap balls of type t0 and t1 between the two containers, we’ll never end up with one container only containing type t0 and the other container only containing type t1. Thus, we print Impossible on a new line.

SOLUTION

As per the given problem, it is given that we can swap between two containers. Let every container ci, contains totalBalli balls , which is the sum of all the elements of the ith row.
And a total number of each type of j balls be ballj, which can be calculated by summing the jth column.

Now for every i,j if totalBalli = = ballj

Then it is poosible otherwise impoosible.

So simply take two arrays find the count of totalBalls and balls.

Sort the array and compare to each corresponding value.

CODE

#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
int main() {
int t; cin >> t;
while( t-- ) {
int n; cin >> n;
ll row[n], col[n];
int i, j;
for(i=0; i<n; i++) row[i] = col[i] = 0;
for(i=0; i<n; i++ ) {
for(j=0;j<n;j++) {
int x; cin >> x;
row[i] += x; col[j] += x;
}
}
sort(row, row+n);
sort(col, col+n);
bool ok = true;
for(i=0; i<n ;i++) {
if(row[i] != col[i]) {
ok = false;
break;
}
}
if(ok==true)
cout<<"Possible"<<endl;
else
cout<<"Impossible"<<endl;
}
return 0;
}

--

--

Ashish Patel
Codebrace

Big Data Engineer at Skyscanner , loves Competitive programming, Big Data.