Organizing Containers of Balls Solution in C, C++, Java, Python

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 <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/

 

Leave a Comment