Cost of Balloons (Hackerearth)

Problem

You are conducting a contest at your college. This contest consists of two problems and  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.




Approach -  First we input all the values and then iterate loops to find if jth element of array [i][j] is 1 or not, 1 represents the task has been completed and we will increment the number of balloons to be provided. 

Finally we find the final cost by multiplying the minimum cost and maximum count of balloons for green and purple balloons. 

Code-

#include <bits/stdc++.h>

using namespace std;

int main(){
int t;
cin >> t;
while(t--){

int green = 0, purple = 0, n, total_green = 0, total_purple = 0;
cin>>green;
cin>>purple;
cin>>n;

int arr[n][2];
for(int i=0; i<n; i++){
for(int j=0; j<2; j++){
cin >> arr[i][j];
}
}
for(int i=0; i<n; i++){
if(arr[i][0] == 1){
total_green = total_green + 1;
}
if(arr[i][1] == 1){
total_purple = total_purple + 1;
}
}
int min_cost = min(green, purple);
int max_cost = max(green, purple);
int min_count = min(total_green, total_purple);
int max_count = max(total_green, total_purple);
int cost = min_cost * max_count + max_cost * min_count;
cout << cost << endl;
}
return 0;
}

Popular posts from this blog

Finding the Subarrays (Hackerearth)

Palindrome Index (Hackerrank)

Sherlock and Array (Hackerrank)