Competitive Coding : Cost of balloons October 16, 2020 by shekhar


Problem Statement:



You are conducting a contest at your college. This contest consists of two problems and n participants. You know the problem that a candidate will solve during the contest.
You provide a balloon to a participant after he or she solves a problem. There are only green and purple-colored balloons available in a market. Each problem must have a balloon associated with it as a prize for solving that specific problem. You can distribute balloons to each participant by performing the following operation:
1. Use green-colored balloons for the first problem and purple-colored balloons for the second problem
2. Use purple-colored balloons for the first problem and green-colored balloons for the second problem
You are given the cost of each balloon and problems that each participant solve. Your task is to print the minimum price that you have to pay while purchasing balloons.
Input format
• First line: T that denotes the number of test cases (1≤T≤10)
• For each test case:
o First line: Cost of green and purple-colored balloons
o Second line: n that denotes the number of participants (1≤n≤10)
• Next n lines: Contain the status of users. For example, if the value of the jth integer in the ith row is 0, then it depicts that the ith participant has not solved the jth problem. Similarly, if the value of the jth integer in the ith row is 1, then it depicts that the ith participant has solved the jth problem.
Output format
For each test case, print the minimum cost that you have to pay to purchase balloons.

If you don’t need the explanation, just scroll down to the end of this page to get the full code for this question. If you have any doubts, you can ask me in comment section.

Explanation:

Basically what the question says is
1. There are ‘N’ number of participants for a competition.
2. There are 2 problems and balloons of 2 colours (Green and Purple).
3. Now you have to give the balloons as prize to the participants, whoever solves at least one problem.
4. You have to choose a particular coloured balloon as prize for a particular question whichever costs less. i.e

  • You can give green coloured balloons for solving 1st question and purple coloured balloons for solving second question.
  • You can give purple coloured balloons for solving 1st question and green coloured balloons for solving second question.

  • Inputs and Outputs:

    The inputs are
    1. ‘T’ : Number of testcases.
    2. For each test cases there will be

  • 3 integer inputs (Cost of Green Balloon, Cost of Purple Balloon and Number of participants)
  • 1 integer array input(having dimension N(no. of particiants)*2(no. of problems)) which contains the information about result of the competition. If the participant has solved only question 1(1 0),only question 2(0 1),both question 1 and 2(1 1) or has not solved any question(0 0).
  • The output is the minimum cost of balloons you have to buy.


    Let’s Code it:

    Step1 : Take the Inputs(explanation for each lines of code is written as comments)

    img2 : Seating Arrangement

    Step2 : Define a method which takes the result array, cost of two types of balloon and number of participants as input and return the total cost as output. The logic is just to iterate the array ,multiply the first column with first price and 2nd column with Second price and finally find the sum of all costs and return the value(total cost). Use static keyword in the method if you don’t want to create instance of the class to call the method. If you want to create an object remove the static keyword and call the method by creating an object of the class.

    img2 : Seating Arrangement
    >

    Step3 : Now the only thing left is getting the output and printing it.
    We have to find the minimum cost. The selection of prizes can be done in 2 possible ways i.e

  • Green coloured balloons for solving 1st question and purple coloured balloons for solving second question.
  • Purple coloured balloons for solving 1st question and green coloured balloons for solving second question.
  • We can do that by calling the method 2 times by alternating the prices of 2 balloons(2nd and 3rd parameters of the method) and then by comparing the two values we can get the minimum cost and print that value.

    img2 : Seating Arrangement

    Full code (Open in Laptop or use Desktop view in Smartphone) :

    import java.util.Scanner;
    class TestClass {
        public static void main(String args[] ) throws Exception {
            Scanner sc = new Scanner(System.in);
            int T = sc.nextInt();
            for(int k = 0; k < T ; k++ ){
                int green_cost = sc.nextInt();
                int purple_cost = sc.nextInt();
                int n = sc.nextInt();
                int[][] solve = new int[n][2];
                for(int i = 0; i < n; i++){
                    for(int j = 0; j < 2; j++){
                        solve[i][j] = sc.nextInt();
                    }
                }
                int amt1 = get_amt(solve,green_cost,purple_cost,n);
                int amt2 = get_amt(solve,purple_cost,green_cost,n);
                if(amt1 < amt2){
                    System.out.println(amt1);
                }
                else{
                    System.out.println(amt2);
                }
            }
        }
        public static int get_amt(int[][] arr, int p1, int p2,int n){
                int amt = 0;
                for(int i = 0; i < n; i++){
                    for(int j = 0; j < 2; j++){
                        if(j == 0){
                            amt += (arr[i][j] * p1);
                        }
                        else{
                            amt += (arr[i][j] * p2);
                        }
                    }
                }
                return amt;
            }
    }

    Leave a Reply

    Your email address will not be published. Required fields are marked *