See the question on Hacker Earth
Difficulty: Easy Problem 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: T 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: 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. 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 Time Limit: 1 Memory Limit: 256 Source Limit: Explanation -
My Solution
T = int(input()) # num of test cases for i in range(T): costs = input().split() costs = [int(c) for c in costs] min_cost = 0 max_cost = 0 if(costs[0] >= costs[1]): max_cost = costs[0] min_cost = costs[1] else: max_cost = costs[1] min_cost = costs[0] n = int(input()) # num of participants x_cnt = 0 y_cnt = 0 for j in range(n): status = input().split() if(status[0] == '1'): x_cnt += 1 if(status[1] == '1'): y_cnt += 1 if(x_cnt <= y_cnt): print(x_cnt * max_cost + y_cnt * min_cost) else: print(x_cnt * min_cost + y_cnt * max_cost)
Critique
Your solution works well and produces the correct output for the given problem. Let's evaluate it in terms of correctness, readability, and opportunities for improvement.
✅ Problem Understanding (Recap)
Each participant may solve:
-
Problem 1: earns a balloon of one color.
-
Problem 2: earns a balloon of another color.
You can:
-
Assign green to problem 1 and purple to problem 2, or
-
Assign purple to problem 1 and green to problem 2.
The goal is to minimize the total cost of the balloons, depending on which problem participants solved and the balloon cost.
✅ Correctness
Your code correctly:
-
Reads costs and number of participants.
-
Counts how many times each problem was solved (
x_cnt
,y_cnt
). -
Chooses the cheaper mapping between balloon colors and problems.
It produces correct results for the provided test cases. ✅
๐ Critique & Suggestions
1. ❌ Misleading variable naming: max_cost
and min_cost
-
You’re not actually comparing costs to pick min/max based on which problem was solved more often.
-
What you should do is compute two total costs and pick the minimum.
✅ Improvement: Calculate both options directly and pick the cheaper one.
2. ๐ Redundant logic for determining cheaper color
-
Your approach compares costs and conditionally assigns
min_cost
andmax_cost
, then compares counts, which is logically flawed in some edge cases.
✅ Better to always calculate both cost combinations explicitly, as shown above.
3. ๐งผ Readability
-
Rename
x_cnt
→problem1
-
Rename
y_cnt
→problem2
It improves clarity and aligns with the problem.
๐งฎ Complexity Analysis
⏱ Time Complexity:
-
For each test case:
O(n)
-
Total:
O(T * n)
-
With
T ≤ 10
,n ≤ 10
→ worst case = 100 lines = trivial runtime
-
๐ง Space Complexity:
-
O(1) (only counters and a few integers used)
✅ Summary
Aspect | Rating / Comment |
---|---|
✅ Correctness | ✅ Works perfectly |
❌ Efficiency | ✅ Fine due to low constraints |
❌ Clarity | Variable naming is misleading |
❌ Flexibility | Hardcoded min/max assignment logic is unnecessary |
✅ Readability | Could be improved |
✅ Suggested Fix | Compute both cost options and pick minimum |
๐ Final Suggestion: Use this simplified version
Let me know if you’d like this converted to another language (e.g., C++ or Java) or want to add test case automation.