Review of 2 Problems
In the problem Maximum of all subarrays of size k, here we find the maximum in each subarray of length k. First you initially consider the first subarray and calculate its max, now when the new incoming element comes we need to check few things and those are:
So checking the tail element whether its max or not does save us lot of unecessary traversals!!
Another Problem that i would like to talk about is Length of the longest substring. Here we need to fing the substring that has unrepeated characters in it. Here we fix two pointers p1 and p2 at the starting of the string and move pointer p2 ahead and add each element in the set and check if its not already present. Initialize the max as 1 and each time you see the the element that is already present in the set, you update your max value and clear the set.
The key step here is that once we come across the element that is already in the set, we move pointer p1 to the next element of that repeated element. say "abedfrg" and the incoming character is e ,then we need to move the p1 and p2 at d.