Skip to main content

Next Greater Element II

Problem​

Given a circular integer array nums (i.e., the next element of nums[nums.length - 1] is nums[0]), return the next greater number for every element in nums.

The next greater number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, return -1 for this number.

Examples​

Example 1:

Input: nums = [1,2,1]
Output: [2,-1,2]
Explanation: The first 1's next greater number is 2;
The number 2 can't find next greater number.
The second 1's next greater number needs to search circularly, which is also 2.

Example 2:

Input: nums = [1,2,3,4,3]
Output: [2,3,4,-1,4]

Constraints​

  • 1 <= nums.length <= 10^4.
  • -10^9 <= nums[i] <= 10^9

Solution for Next Greater Element II​

Brute Force - Recursion​

Intuition​

The idea is to make use of an array doublearr which is formed by concatenating two copies of the given array one after the other. Now, when we need to find out the next greater element for arr[i], we can simply scan all the elements doublearr[j]. The first element found satisfying the given condition is the required result for arr[i]. If no such element is found, we put a -1 at the appropriate position in the res array.

Implementation​

  • Create an empty vector to store the next greater element for each element in the input array. Duplicate the input array to create a circular array. This is done by appending the original array to itself.
  • Start iterating through each element in the original array.
  • For each element in the original array, search for the next greater element in the circular array starting from the next position after the current element.
  • If a greater element is found, update the corresponding index in the result vector with the value of the greater element. If no greater element is found, keep the index in the result vector as -1.
  • Once all elements have been processed, return the result vector containing the next greater element for each element in the input array.

Brute Force Solution​

Implementation​

class Solution {
public:
vector<int> nextGreaterElement(int N, vector<int>& arr) {
vector<int> res(N, -1);
vector<int> doublearr(arr.begin(), arr.end());
doublearr.insert(doublearr.end(), arr.begin(), arr.end());
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < doublearr.size(); j++) {
if (doublearr[j] > doublearr[i]) {
res[i] = doublearr[j];
break;
}
}
}
return res;
// code here
}
};

Code in Different Languages​

Written by @himanshukumar
class Solution:
def nextGreaterElement(self, N, arr):
res = [-1] * len(arr)
doublearr = arr + arr
for i in range(len(arr)):
for j in range(i + 1, len(doublearr)):
if doublearr[j] > doublearr[i]:
res[i] = doublearr[j]
break
return res

Complexity Analysis​

  • Time Complexity: O(N2)O(N^2)
    • The function iterates through each element in the input array, resulting in O(N)O(N) iterations. For each element, it searches for the next greater element in the circular array, which may require iterating through the entire circular array in the worst case, resulting in another O(N)O(N) iterations. Therefore, the overall time complexity is O(N2)O(N^2).
  • Space Complexity: O(N)O(N)
    • The function creates a duplicate circular array of size 2N2N to handle cases where the next greater element wraps around to the beginning of the array. Therefore, the additional space required is O(N)O(N) to store this duplicate array.
    • Additionally, the result vector to store the next greater element for each element in the input array also requires O(N)O(N) space. Therefore, the overall space complexity is O(N)O(N).

References​