Notice
The following content is the property of Swayam used for explaining the solution. All rights reserved by Swayam and their publishers. Do not copy the solution directly.
1. Problem Description
You are selecting courses for your next semester, where the university offers x courses that you are eligible to take. Each course requires a certain number of hours per week. Given your other commitments, you have h hours available in your week for courses. You need to choose 2 courses that will allow you to have the most free time remaining for other activities and clubs.
Your task is to choose the best two courses such that the sum of their weekly hours leaves you with the maximum possible free time. If no such pair of courses can fit within your available hours, you should skip the semester.
1.1 Input Format
The input consists of multiple test cases, with the following structure:
- First line: A single integer t, the number of test cases.
- For each test case:
- The first line contains two space-separated integers x and h.
- The second line contains x space-separated integers Ci, representing the weekly hours required for each course.
1.2 Constraints
- 2 < x < 108
- 1 < h < 168
- 1 < Ci < 1015 for all i ∈ [1, x]
1.3 Output Format
For each test case, output a single integer y:
- If there are no two courses that can fit within the available hours h, output h (indicating that you skip the semester).
- Otherwise, output the maximum remaining free time you would have each week after selecting the two courses.
1.4 Sample Input
2
3 3
3 2 3
3 3
1 2 2
1.5 Sample Output
3
0
1.6 Explanation
- In the first test case, there are no two courses that can be taken together without exceeding the available 3 hours per week. Therefore, the output is h, which is 3, indicating you should skip the semester.
- In the second test case, the best choice is to take courses with 1 and 2 hours per week, which exactly matches your available time, leaving you with 0 hours of free time. Hence, the output is 0.
Solution
# Read the number of test cases (t) from user input
t = int(input())
# Loop through each test case
for _ in range(t):
# Read two integers x and h from user input. x is unused, h is the target value.
x, h = map(int, input().split())
# Read a list of integers from user input and store them in list 'c'
c = list(map(int, input().split()))
# Sort the list 'c' in ascending order
c.sort()
# Get the smallest element in the sorted list
min1 = c[0]
# Get the second smallest element in the sorted list
min2 = c[1]
# Check if the sum of the two smallest elements is less than or equal to h
if (min1 + min2) <= h:
# If yes, print the difference between h and the sum of the two smallest elements
print(h - (min1 + min2))
else:
# Otherwise, just print h
print(h)
Sample Solution Provided by Instructor
t = int(input()) # Read the number of test cases.
# Loop through each test case.
for _ in range(t):
# Read two integers 'x' and 'h' from the input.
# 'x' is not used in the current implementation.
# 'h' represents the initial value of a variable that will be adjusted based on the smallest values in the list 'C'.
x, h = map(int, input().split())
# Read a list of integers 'C' which contains the elements that we will work with.
C = list(map(int, input().split()))
# Find the smallest element in the list 'C' and store it in 'm1'.
m1 = min(C)
# Remove the smallest element 'm1' from the list 'C'.
C.remove(m1)
# Find the next smallest element in the updated list 'C' and store it in 'm2'.
m2 = min(C)
# The following line checks if the sum of the two smallest elements ('m1' and 'm2') is less than or equal to 'h'.
# If the sum is less than or equal to 'h', it subtracts the sum of 'm1' and 'm2' from 'h'.
# Otherwise, it leaves 'h' unchanged.
# This adjusted value is then printed.
print(h - m1 - m2 if h >= m1 + m2 else h)