I have a problem. Given an array, find the smallest integer and the second smallest integer for all possible subarrays, where a subarray is a contiguous subset of the original array.
Consider for example an array A = [5, 6, 1, 2, 9, 3]. Obviously the subarray size would be atleast 2, so we have a total of (n) * (n+1) /2 - n subarrays. ( subtracting n subarrays of size 1 from total). That is a straighforward O(n^2) solution, checking each subarray and recording the required integers. But i think it can be done in O(n) using stacks. But i am unable to use it intuitively to arrive at an optimal solution. Any help, advice, direction would be appreciated.