Progcomp Training Week 2
Progcomp Training Week 2
Viney Kumar
16/08/2019
Observation 1
There are N − w + 1 possible sub-arrays, and therefore N − w + 1 possible starting locations for the sub-arrays.
Explanation
Let’s find the starting location for the last sub-array. Clearly, this sub-array must consist of the last w
houses in the array of N elements. Therefore, the starting location must be w − 1 houses away from house N
(assuming houses numbered 1 to N ).Therefore, the starting position of the last sub-array (and therefore the
number of possible starting points) is N − w + 1.
Idea (Algorithm)
1
How many marks would this solution score?
In order to work this out, we need to be able to do some big-oh analysis of the running time of our program.
Here’s how you might go about it:
1. There are N − w + 1 starting points. For each starting point, we are looping through w elements to
find the sum of the current sub-array.
2. This means that the main computation takes approximately w ∗(N −w +1) basic operations to complete,
and each of these operations (adding, comparisons ect) takes constant (in other words O(1) ) time.
3. Now take a look at the constraints section in the problem statement. The constraints are 1 <= N <=
100, 000 and 1 <= w <= N . Let’s apply the second constraint, which says that 1 <= w <= n. Hence
w could be equal to N/2. In that case, the number of operations (which we previously showed was
approximately w ∗ (N − w + 1) ), is now equal to (N/2) ∗ (N − (N/2) + 1) = (N/2) ∗ (1 + N/2). Hence,
in the worst case, the number of operations is a quadratic function of the input N ! Therefore, our
algorithm runs in O(N 2 ) time.
4. How many points would such an algorithm score? Recall that N can be at most 100,000, and
(105 )2 = 101 0 operations, which is much too big to fit in the time limit of 1 second. Remember, a
computer can only do 50 million= 5 ∗ 107 operations per second. As a result, this algorithm would
time out on the largest testcases. However, there is also some good news. Recall that for 50% of the
available marks, 1 <= N <= 1000. For the case of N = 1000, our algorithm would execute about
10002 = 106 = 1 million operations, which comfortably fits in the allocated time limit of 1 second.
5. Therefore, our approach (if coded with no errors) will score at most 50/100 marks.
Observation 2
When we sum up the sub-arrays, a lot of the elements are repeated from the previous sum. For example, in
the sample input, when we sum up [3, 2, 5, 1] and [2, 5, 1, 4], we are repeating the computation 2 + 5 + 1 twice!
In fact, if we already have the first sum, we could have simply subtracted 3 and added 4 to get the next sum!
Idea 2
Using this observation we can modify our previous brute force in the following way: We compute the sum of
the first w elements as in the brute force method. However, for the rest of the sub-arrays, we simply subtract
and add the respective elements to get the next sum! This addition and subtraction takes constant time,
which means that we only need one loop! As a result, the running time of a program of this form will be
O(n) or linear time.
Exercise
Implement such an approach on your own and try to score 100% (or close to 100% if you have some minor
bugs) on the test cases.
2
Moral of the Story
The technique where we use clever manipulations to avoid an extra nested loop is known as the “Sliding
Window technique”, and is applicable to many other problems too! Here are some more examples of sliding
window problems for you to practice on:
A brute force solution would involve going through every possible pair (or slice) from the list, checking
whether the beam could cover the interval, and then checking how many humans were covered in order to
obtain the maximum possible number of humans covered by the beam. This takes quadratic time (i.e O(n2 )
time) and will score 60%.
To score full marks, a sliding window solution is necessary. Is there a way to use the fact that the numbers
are already sorted to our advantage?