David has several containers, each with a number of balls in it. He has just enough containers to sort each type of ball he has into its own container. David wants to sort the balls using his sort method.
As an example, David has n=2 containers and 2 different types of balls, both of which are numbered from 0 to n-1=1. The distribution of ball types per container are described by an n*n matrix of integers,
In a single operation, David can swap two balls located in different containers.
The diagram below depicts a single swap operation:
David wants to perform some number of swap operations such that:
- Each container contains only balls of the same type.
- No two balls of the same type are located in different containers.
You must perform queries where each query is in the form of a matrix, . For each query, print Possible on a new line if David can satisfy the conditions above for the given matrix. Otherwise, print Impossible.
Function Description
Complete the organizingContainers function in the editor below. It should return a string, either Possible or Impossible.
organizingContainers has the following parameter(s):
- containter: a two dimensional array of integers that represent the number of balls of each color in each container
Input Format
The first line contains an integer , the number of queries.
Each of the next sets of lines is as follows:
- The first line contains an integer , the number of containers (rows) and ball types (columns).
- Each of the next lines contains space-separated integers describing row .
Constraints
- 1<=q<=10
- 1<=n<=100
- 0<=M[container][type]<=10^9
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.
Sample Input 0
2 2 1 1 1 1 2 0 2 1 1
Sample Output 0
Possible Impossible
TABLE OF CONTENTS
Organizing Containers of Balls Solution in C
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int main() { int t; scanf("%d", &t); while(t--){ int n, i, j; scanf("%d", &n); int a[n][n]; int *sum1 = (int *)calloc(sizeof(int), n); for(i = 0; i < n; i++){ for(j = 0; j < n; j++){ scanf("%d", &a[i][j]); sum1[i] += a[i][j]; } } for(i = 0; i < n; i++){ int sum2 = 0; for(j = 0; j < n; j++){ sum2 += a[j][i]; } for(j = 0; j < n; j++) if (sum1[j] == sum2){ sum1[j] = 0; break; } if (j == n){ printf("Impossible\n"); break; } } if (i == n) printf("Possible\n"); } /* Enter your code here. Read input from STDIN. Print output to STDOUT */ return 0; }
Organizing Containers of Balls Solution in C++
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <string> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <climits> #include <cstring> #include <cstdlib> #include <fstream> #include <numeric> #include <sstream> #include <iostream> #include <algorithm> #include <unordered_map> using namespace std; int main(){ int q; cin >> q; for(int a0 = 0; a0 < q; a0++){ int n; cin >> n; vector< vector<int> > M(n,vector<int>(n)); long long totalIn[101]={0},totalOf[100]={0}; for(int M_i = 0;M_i < n;M_i++){ for(int M_j = 0;M_j < n;M_j++){ cin >> M[M_i][M_j]; totalIn[M_i]+=M[M_i][M_j]; totalOf[M_j]+=M[M_i][M_j]; } } sort(totalIn,totalIn+100); sort(totalOf,totalOf+100); int i; for(i=0;i<100;i++) { if(totalIn[i]!=totalOf[i]) break; } if(i==100) cout<<"Possible"<<endl; else cout<<"Impossible"<<endl; // your code goes here } return 0; }
Organizing Containers of Balls Solution in Java
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int q = in.nextInt(); for(int a0 = 0; a0 < q; a0++){ int n = in.nextInt(); int[][] M = new int[n][n]; for(int M_i=0; M_i < n; M_i++){ for(int M_j=0; M_j < n; M_j++){ M[M_i][M_j] = in.nextInt(); } } int[] rt = new int[n]; int[] ct = new int[n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { rt[i] += M[i][j]; ct[j] += M[i][j]; } } Arrays.sort(rt); Arrays.sort(ct); String ans = "Possible"; for (int i = 0; i < n; i++) { if (rt[i] != ct[i]) ans = "Impossible"; } System.out.println(ans); } } }
Organizing Containers of Balls Solution in Python
#!/bin/python import sys q = int(raw_input().strip()) for a0 in xrange(q): n = int(raw_input().strip()) M = [] for M_i in xrange(n): M_temp = map(int,raw_input().strip().split(' ')) M.append(M_temp) ballcounts = {} for j in xrange(n): s = 0 for i in xrange(n): s += M[i][j] if s in ballcounts: ballcounts[s] += 1 else: ballcounts[s] = 1 conts = {} for i in xrange(n): s = 0 for j in xrange(n): s += M[i][j] if s in conts: conts[s] += 1 else: conts[s] = 1 poss = True #for x in ballcounts: # if ballcounts[x] % 2 == 1: # poss = False for x in ballcounts: if not (x in conts): poss = False break if conts[x] != ballcounts[x]: poss = False break for x in conts: if not (x in ballcounts): poss = False break if conts[x] != ballcounts[x]: poss = False break if (poss): print "Possible" else: print "Impossible"
Organizing Containers of Balls Solution in C#
using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Linq.Expressions; using System.Text; namespace CF317 { internal class Program { private static void Main(string[] args) { var sr = new InputReader(Console.In); //var sr = new InputReader(new StreamReader("input.txt")); var task = new Task(); using (var sw = Console.Out) //using (var sw = new StreamWriter("output.txt")) { task.Solve(sr, sw); //Console.ReadKey(); } } } internal class Task { public void Solve(InputReader sr, TextWriter sw) { var tests = sr.NextInt32(); for (var t = 0; t < tests; t++) { var n = sr.NextInt32(); var matrix = new long[n, n]; for (var i = 0; i < n; i++) { var input = sr.ReadArray(Int64.Parse); for (var j = 0; j < n; j++) { matrix[i, j] = input[j]; } } var containersCap = new Dictionary<long, int>(); for (var i = 0; i < n; i++) { var currCap = 0L; for (var j = 0; j < n; j++) { currCap += matrix[i, j]; } if (!containersCap.ContainsKey(currCap)) { containersCap.Add(currCap, 0); } containersCap[currCap]++; } var typesCap = new Dictionary<long, int>(); for (var j = 0; j < n; j++) { var curr = 0L; for (var i = 0; i < n; i++) { curr += matrix[i, j]; } if (!typesCap.ContainsKey(curr)) { typesCap.Add(curr, 0); } typesCap[curr]++; } var answ = "Possible"; foreach (var item in typesCap) { if (!containersCap.ContainsKey(item.Key)) { answ = "Impossible"; break; } containersCap[item.Key] -= item.Value; if (containersCap[item.Key] != 0) { answ = "Impossible"; break; } containersCap.Remove(item.Key); } if (containersCap.Count != 0) { answ = "Impossible"; } sw.WriteLine(answ); } } } } internal class InputReader : IDisposable { private bool isDispose; private readonly TextReader sr; public InputReader(TextReader stream) { sr = stream; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } public string NextString() { var result = sr.ReadLine(); return result; } public int NextInt32() { return Int32.Parse(NextString()); } public long NextInt64() { return Int64.Parse(NextString()); } public string[] NextSplitStrings() { return NextString() .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); } public T[] ReadArray<T>(Func<string, CultureInfo, T> func) { return NextSplitStrings() .Select(s => func(s, CultureInfo.InvariantCulture)) .ToArray(); } public T[] ReadArrayFromString<T>(Func<string, CultureInfo, T> func, string str) { return str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .Select(s => func(s, CultureInfo.InvariantCulture)) .ToArray(); } protected void Dispose(bool dispose) { if (!isDispose) { if (dispose) sr.Close(); isDispose = true; } } ~InputReader() { Dispose(false); } }
Attempt Organizing Containers of Balls HackerRank Challenge
Link – https://www.hackerrank.com/challenges/taum-and-bday/
Next HackerRank Challenge Solution
Link – https://exploringbits.com/encryption-hackerrank-solution/