This post explains the HackerEarth problem Cost of balloons step by step: the problem statement, input and output format, constraints, a sample test case and a working solution in Java that you can study and run.

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:
Use green-colored balloons for the first problem and purple-colored balloons for the second problem
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: that denotes the number of test cases (1<=T<=10)
For each test case:
First line: Cost of green and purple-colored balloons
Second line: that denotes the number of participants (1<=N<=10)
Next 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 integer in the row is 1 , then it depicts that the participant has solved the problem.
Output format
For each test case, print the minimum cost that you have to pay to purchase balloons.
Sample Input
2
9 6
10
1 1
1 1
0 1
0 0
0 1
0 0
0 1
0 1
1 1
0 0
1 9
10
0 1
0 0
0 0
0 1
1 0
0 1
0 1
0 0
0 1
0 0
Sample Output
69
14
Cost of balloons Solution in Java
import java.util.*;
public class baloonsCost {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int T=sc.nextInt();
while(T>0){ //number of test cases
if(T>=1 && T<=10) {
int c1=sc.nextInt();
int c2=sc.nextInt();
int n=sc.nextInt();
int count1=0;
int count2=0;
for(int i=1;i<=n;i++) {
int f=sc.nextInt();
if(f==1) {
count1++;
}
int s=sc.nextInt();
if(s==1) {
count2++;
}
}
int min1=count1*c1+count2*c2;
int min2=count1*c2+count2*c1;
int min=(min1<min2?min1:min2);
System.out.println(min); // returning mimimum cost
}
else
System.out.println(-1);
T--;//ending the while loop
}
}
}
If you have any doubts regarding this problem or need the solution in other programming languages then leave a comment down below .
Tip: try to solve the problem yourself before reading the solution. Use the code as a reference to check your approach, and make sure you understand why it works.
Is program in c language
ReplyDelete