I\'m given an array a[n][2]
, where n
can be 10^5
at max. There are n subjects and n students. All numbered 1, 2, ..., n.
a[i
Didn't quite understand your code, here is an alternative approach. This is similar to an interval problem. Let's take your example:
We first make an array of size as no. of subjects + 1 (for the sake of simplicity in computation).
1 2 3 4 5 6
+1
to the starting point and -1
to the ending point + 1th location.Array representation:(after the entire above process).
1 2 3 4 5 6
+1 -1 [1,3]
+1 -1 [1,3]
+1 -1 [1,5]
+1 -1 [1,5]
+1 -1 [1,5]
Summing looks like:
1 2 3 4 5 6
+1 -1 [1,3]
+1 -1 [1,3]
+1 -1 [1,5]
+1 -1 [1,5]
+1 -1 [1,5]
5 5 5 3 3 0(not considered anyway)