Organizing containers of balls hackerrank solution

It is maybe not so evident from the description of the challenge that the containers can have different capacities. All the examples that were given had same-sized containers. To better grasp the problem, I find it useful to work with an example that has different sized containers:

\ball types | red | blue | green containers\ | | | ---------------------+-----+------+------- A | 2 | 3 | 1 B | 4 | 0 | 1 C | 1 | 3 | 3

So we have 3 containers (named A, B and C), and 3 types of balls (red, blue, green). Note that container B is the smallest: apparently it can contain 5 balls. A can contain 6 balls, and C can contain 7 balls.

A key observation is that if we first empty the containers and then freely distribute the balls over the containers, filling them again, there is always a way to achieve that same transition through mere swapping. You may need a moment to let this sink in and to verify this is indeed the case. But once you get this principle, you can just forget about the swapping part, and approach the problem from an angle where you first remove all the balls and then distribute them again.

So let's empty the containers. We then have the following balls "in our hands":

red | blue | green -----+------+------- 7 | 6 | 5

Now ask yourself: if container B has a capacity of 5, which kind of balls would you put in there? Obviously the green balls. It wouldn't work with any other type of ball, since you would then have to put one or more of them in another container, which violates the requirements.

The general rule is thus: put the balls of which you have the least in the container that has the smallest capacity. If that number of balls doesn't match the capacity, then there is just no way to get to a solution. If it does match, then you can repeat this with the next kind of balls (in increasing order) and next container (in increasing capacity). And so you see that actually sorting the containers and balls like that is a way to find a solution.

Taum is planning to celebrate the birthday of his friend, Diksha. There are two types of gifts that Diksha wants from Taum: one is black and the other is white. To make her happy, Taum has to buy b black gifts and  white gifts.

  • The cost of each black gift is bc units.
  • The cost of every white gift is wc units.
  • The cost to convert a black gift into white gift or vice versa is  units.

Determine the minimum cost of Diksha’s gifts.

Example

b=3

w=5

bc=3

wc=4

z=1

He can buy a black gift for 3 and convert it to a white gift for 1, making the total cost of each white gift 4. That matches the cost of a white gift, so he can do that or just buy black gifts and white gifts. Either way, the overall cost is 3*3+5*4=29.

Function Description

Complete the function taumBday in the editor below. It should return the minimal cost of obtaining the desired gifts.

taumBday has the following parameter(s):

  • int b: the number of black gifts
  • int w: the number of white gifts
  • int bc: the cost of a black gift
  • int wc: the cost of a white gift
  • int z: the cost to convert one color gift to the other color

Returns

  • int: the minimum cost to purchase the gifts

Input Format

The first line will contain an integer t, the number of test cases.

The next t pairs of lines are as follows:

– The first line contains the values of integers b and w.

– The next line contains the values of integers bc,wc , and z.

Constraints

1<=t<=10

0<=b,w,b,wc,z<=10^9

Output Format

 lines, each containing an integer: the minimum amount of units Taum needs to spend on gifts.

Sample Input

STDIN   Function -----   -------- 5       t = 5 10 10   b = 10, w = 10 1 1 1   bc = 1, wc = 1, z = 1 5 9     b = 5, w = 5 2 3 4   bc = 2, wc = 3, z = 4 3 6     b = 3, w = 6 9 1 1   bc = 9, wc = 1, z = 1 7 7     b = 7, w = 7 4 2 1   bc = 4, wc = 2, z = 1 3 3     b = 3, w = 3 1 9 2   bc = 1, wc = 9, z = 2

Sample Output

20 37 12 35 12

Organizing Containers of Balls Solution in C++

#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; int main(){ int q; cin >> q; for(int a0 = 0; a0 < q; a0++){ int n; cin >> n; vector< vector > M(n,vector(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"<Organizing Containers of Balls Solution in Javaimport 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(); 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(); 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(Func func) { return NextSplitStrings() .Select(s => func(s, CultureInfo.InvariantCulture)) .ToArray(); } public T[] ReadArrayFromString(Func 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/