Week 3 - Assignment 1 - CS103 - Swayam

Week 3 - Assignment 1

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:

1.2 Constraints

1.3 Output Format

For each test case, output a single integer y:

1.4 Sample Input

2
3 3
3 2 3
3 3
1 2 2

1.5 Sample Output

3
0

1.6 Explanation

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)