
Sponsored
Sponsored
This approach involves joining the Students and Subjects tables to generate all combinations of students and subjects. Then, we use a left join with the Examinations table to count how many times each student attended each exam. Group By is used to aggregate and count the occurrences, ensuring that all combinations are accounted for.
Time Complexity: O(N * M + E), where N is the number of students, M is the number of subjects, and E is the number of examination records.
Space Complexity: O(N * M), for storing the cross join results before joining with Examinations.
1SELECT s.student_id, s.student_name, sub.subject_name, COUNT(e.student_id) AS attended_exams FROM Students s CROSS JOIN Subjects sub LEFT JOIN Examinations e ON s.student_id = e.student_id AND sub.subject_name = e.subject_name GROUP BY s.student_id, s.student_name, sub.subject_name ORDER BY s.student_id, sub.subject_name;The solution begins with a cross join between the Students and Subjects tables to list every student-subject combination. Then, a left join is performed with the Examinations table on both student_id and subject_name to account for the attendance. We count these occurrences with COUNT(e.student_id) and group the results by student and subject. Finally, we order the results by student_id and subject_name.
This approach uses nested queries to first produce all combinations of students and subjects. The inner query selects results from the Examinations table and the outer query ensures all zero-attendance cases are captured properly using a union operation with defaults.
Time Complexity: O(N * M + E), where N is the number of students, M is the number of subjects, and E is the number of examination records.
Space Complexity: O(N * M), for storing the resulting dataset.
1SELECT Students.student_id, Students.student_name, Subjects.subject_name, COALESCE(Exams.attended_exams, 0This approach involves using a hash map (also called a dictionary in some languages) to keep track of the elements we've seen so far and their counts or indices. This allows us to efficiently find the solution in linear time.
Time Complexity: O(n).
Space Complexity: O(n).
1defThis approach involves first sorting the array and then using a two-pointer technique to find the two numbers that sum to the target. This can be more space-efficient but requires sorting, which takes O(n log n) time.
Not applicable for this solution in C.
1CodeHere, we perform a cross join between Students and Subjects to list all student-subject pairs. A nested query inside the left join counts the occurrences of each student-subject appearing in the Examinations table. COALESCE is used to replace NULL with 0 for cases where a student did not attend a particular subject. Finally, results are ordered by student_id and subject_name.
This Python solution uses a dictionary to map each number to its index, allowing us to quickly find the complement of the current number.
Not including code for Java in this approach as it is similar in concept to C++ and requires additional structures to maintain indices.