The problem. Given a circular array nums — after the last element comes the first one again — return the next greater number for every element: the first number bigger than it when you walk forward, wrapping around the end. If no number in the whole circle is bigger, the answer is -1. Values may repeat.
Input: nums = [1, 2, 1]
Output: [2, -1, 2] the last 1 wraps around to find 2
Input: nums = [1, 2, 3, 4, 3]
Output: [2, 3, 4, -1, 4]This is Next Greater Element with one twist: the search may wrap around. Without the wrap, the last element would always get -1.
Free account
Sign up to read the rest of this lesson: 5 more sections, 3 drawings, a dry-run simulator and code in JavaScript, Python, Java and C++.
Still to come