-
Notifications
You must be signed in to change notification settings - Fork 111
/
solution.cpp
77 lines (56 loc) · 1.93 KB
/
solution.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
73
74
75
76
77
/*
Solution: The logic in this problem is that after all the swap operations, the number of balls inside the
container reamins the same.
So, all we have to do is to check if there exists a ball whose ball_count is equal to a particular container's
capacity. If it exists, then we will similarly check for remaining balls. Otherwise, we return "Impossible".
So,
1. Create two array (capacity, ball_count) and fill those arrays by traversing the 2-D matrix.
2. Sort the two arrays and check if they are the same or not.
3. If they are same, return "Possible", else return "Impossible".
*/
#include <bits/stdc++.h>
using namespace std;
// Complete the organizingContainers function below.
string organizingContainers(vector<vector<int>> container) {
int n = container.size();
vector<int> capacity(n,0);
vector<int> ball_count(n,0);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
capacity[i] += container[i][j];
}
}
for(int j=0;j<n;j++){
for(int i=0;i<n;i++){
ball_count[j] += container[i][j];
}
}
sort(capacity.begin(),capacity.end());
sort(ball_count.begin(),ball_count.end());
if(capacity==ball_count) return "Possible";
else return "Impossible";
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int q;
cin >> q;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int q_itr = 0; q_itr < q; q_itr++) {
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
vector<vector<int>> container(n);
for (int i = 0; i < n; i++) {
container[i].resize(n);
for (int j = 0; j < n; j++) {
cin >> container[i][j];
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
string result = organizingContainers(container);
fout << result << "\n";
}
fout.close();
return 0;
}