Find the next greater element in a Circular Array | Set 2
Last Updated :
26 Aug, 2021
Given a circular array arr[] consisting of N integers, the task is to print the Next Greater Element for every element of the circular array. Elements for which no greater element exist, print “-1”.
Examples:
Input: arr[] = {5, 6, 7}
Output: 6 7 -1
Explanation: The next greater element for every array element are as follows:
For arr[0] (= 5) -> 6
For arr[1] (= 6) -> 7
For arr[2] (= 7), no greater element is present in the array. Therefore, print -1.
Input: arr[] = {4, -2, 5, 3}
Output: 5 5 -1 4
Explanation: The next greater element for every array element are as follows:
For arr[0] (= 4) -> 5
For arr[1] (= -2) -> 5
For arr[2] (= 5), no greater element is present in the array. Therefore, print -1.
For arr[3] (= 3) -> 4
Naive Approach: The simplest approach to solve this problem is discussed in the previous post of this article.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Alternate Approach: Refer to the previous post of this article for space-optimization of the naive approach.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use the concept of Next Greater Element which uses a Stack data structure. Follow the steps below to solve the problem:
- Initialize a stack to store the indices of the array and an array nge[] of size N which stores the next greater element for each array element.
- Traverse the array and for each index, perform the following:
- After a single traversal of N elements, the stack contains the elements which do not have a next greater element till the (N – 1)th index. As the array is circular, consider all the elements from the 0th index again and find the next greater element of the remaining elements.
- Since the array is traversed 2 times, it is better to use i % N instead of i.
- After completing the above steps, print the array nge[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printNGE( int * arr, int n)
{
stack< int > s;
int nge[n], i = 0;
for (i = 0; i < n; i++) {
nge[i] = -1;
}
i = 0;
while (i < 2 * n) {
while (!s.empty()
&& arr[i % n] > arr[s.top()]) {
nge[s.top()] = arr[i % n];
s.pop();
}
s.push(i % n);
i++;
}
for (i = 0; i < n; i++) {
cout << nge[i] << " " ;
}
}
int main()
{
int arr[] = { 4, -2, 5, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
printNGE(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void printNGE( int arr[], int n)
{
Stack<Integer> s = new Stack<>();
int nge[] = new int [n];
int i = 0 ;
for (i = 0 ; i < n; i++)
{
nge[i] = - 1 ;
}
i = 0 ;
while (i < 2 * n)
{
while (!s.isEmpty() &&
arr[i % n] > arr[s.peek()])
{
nge[s.peek()] = arr[i % n];
s.pop();
}
s.push(i % n);
i++;
}
for (i = 0 ; i < n; i++)
{
System.out.print(nge[i] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 4 , - 2 , 5 , 8 };
int N = arr.length;
printNGE(arr, N);
}
}
|
Python3
def printNGE(arr, n) :
s = [];
nge = [ - 1 ] * n;
i = 0 ;
while (i < 2 * n) :
while ( len (s) ! = 0 and arr[i % n] > arr[s[ - 1 ]]) :
nge[s[ - 1 ]] = arr[i % n];
s.pop();
s.append(i % n);
i + = 1 ;
for i in range (n) :
print (nge[i], end = " " );
if __name__ = = "__main__" :
arr = [ 4 , - 2 , 5 , 8 ];
N = len (arr);
printNGE(arr, N);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void printNGE( int []arr, int n)
{
Stack< int > s = new Stack< int >();
int []nge = new int [n];
int i = 0;
for (i = 0; i < n; i++)
{
nge[i] = -1;
}
i = 0;
while (i < 2 * n)
{
while (s.Count != 0 &&
arr[i % n] > arr[s.Peek()])
{
nge[s.Peek()] = arr[i % n];
s.Pop();
}
s.Push(i % n);
i++;
}
for (i = 0; i < n; i++)
{
Console.Write(nge[i] + " " );
}
}
public static void Main(String[] args)
{
int []arr = { 4, -2, 5, 8 };
int N = arr.Length;
printNGE(arr, N);
}
}
|
Javascript
<script>
function printNGE(arr, n)
{
let s = [];
let nge = new Array(n);
let i = 0;
for (i = 0; i < n; i++)
{
nge[i] = -1;
}
i = 0;
while (i < 2 * n)
{
while (s.length != 0 &&
arr[i % n] > arr[s[s.length - 1]])
{
nge[s[s.length - 1]] = arr[i % n];
s.pop();
}
s.push(i % n);
i++;
}
for (i = 0; i < n; i++)
{
document.write(nge[i] + " " );
}
}
let arr = [ 4, -2, 5, 8 ];
let N = arr.length;
printNGE(arr, N);
</script>
|
Output:
5 5 8 -1
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...