-
Notifications
You must be signed in to change notification settings - Fork 0
/
Organizing_Containers_of_Balls.cpp
72 lines (53 loc) · 1.36 KB
/
Organizing_Containers_of_Balls.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Title : Organizing Containers of Balls
* Author : Tridib Samanta
* Link : https://www.hackerrank.com/challenges/organizing-containers-of-balls/problem
**/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
string organizingContainers(vector<vector<ll> > &M) {
int n = M.size();
if (n == 1)
return "Possible";
unordered_map<ll, ll> ump;
for (int i = 0; i < n; ++i) {
ll containerSum = 0;
for (int j = 0; j < n; ++j) {
containerSum += M[i][j];
}
++ump[containerSum];
}
for (int i = 0; i < n; ++i) {
ll typeSum = 0;
for (int j = 0; j < n; ++j) {
typeSum += M[j][i];
}
if (ump[typeSum] == 0)
return "Impossible";
else
--ump[typeSum];
}
return "Possible";
}
int main() {
int q;
cin >> q;
while (q--) {
int n;
cin >> n;
vector<vector<ll> > M(n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
ll count;
cin >> count;
M[i].emplace_back(count);
}
}
string res = organizingContainers(M);
cout << res << '\n';
}
return 0;
}
// T.C.: O(N*N)
// S.C.: O(N+N)