Untitled

mail@pastecode.io avatar
unknown
java
8 months ago
1.9 kB
3
Indexable
Never
public class MaximumNumber {
    // Function to calculate the maximum number of subjects that can be passed
    public static int maxNumSub(List<Integer> answered, List<Integer> needed, int q) {
        int maxPassableSubjects = 0; // Initialize the maximum passable subjects to 0
        int n = answered.size(); // Get the number of subjects

        // Loop through each subject
        for (int i = 0; i < n; i++) {
            int additionalQuestionsNeeded = needed.get(i) - answered.get(i);

            // Check if the student has enough attempts (q) to pass the current subject
            if (additionalQuestionsNeeded <= q) {
                q -= additionalQuestionsNeeded; // Update remaining attempts
                maxPassableSubjects++; // Increment the number of subjects passed
            } else if (i + 1 < n) {
                // If there are more subjects and not enough attempts, check the next subject
                int questionsToPassNextSubject = needed.get(i + 1) - answered.get(i + 1);

                if (questionsToPassNextSubject <= q) {
                    q -= questionsToPassNextSubject; // Update remaining attempts
                    maxPassableSubjects++; // Increment the number of subjects passed
                    i++; // Move to the next subject
                } else {
                    break; // No more subjects can be passed
                }
            } else {
                break; // No more subjects to check
            }
        }

        return maxPassableSubjects; // Return the maximum number of subjects passed
    }

    public static void main(String[] args) {
        List<Integer> answered = List.of(2, 4);
        List<Integer> needed = List.of(4, 5);
        int q = 1;
        int maxPassedSubjects = maxNumSub(answered, needed, q);
        System.out.println("Max number of subjects passed: " + maxPassedSubjects);
    }
}
Leave a Comment