Parentheses matching was easy. Now consider this: for each element in an array, find the next element to the right that is greater.
Array: Answers: For element at index , the next greater is at index .
For element , there's nothing greater to its right, so the answer is . Brute force is . There's an solution using a special type of stack.