Given a circular array arr[] of N integers such that the last element of the given array is adjacent to the first element of the array, the task is to print the Next Greater Element in this circular array. Elements for which no greater element exist, consider the next greater element as “-1”.
Examples:
Input: arr[] = {5, 6, 7}
Output: {6, 7, -1}
Explanation:
Next Greater Element for 5 is 6, for 6 is 7, and for 7 is -1 as we don’t have any element greater than itself so its -1.Input: arr[] = {4, -2, 5, 8}
Output: {5, 5, 8, -1}
Explanation:
Next Greater Element for 4 is 5, for -2 its 5, for 5 is 8, and for 8 is -1 as we don’t have any element greater than itself so its -1, and for 3 its 4.
Approach: This problem can be solved using Greedy Approach. Below are the steps:
- For the property of circular array to be valid append the given array elements to the same array once again.
For Example:Let arr[] = {1, 4, 3}
After appending the same set of elements arr[] becomes
arr[] = {1, 4, 3, 1, 4, 3} - Find the next greater element till N elements in the above array formed.
- If any greater element is found then print that element, else print “-1”.
Below is the implementation of above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the NGE void printNGE(int A[], int n) { // Formation of cicular array int arr[2 * n]; // Append the given array element twice for (int i = 0; i < 2 * n; i++) arr[i] = A[i % n]; int next, i, j; // Iterate for all the // elements of the array for (i = 0; i < n; i++) { // Initialise NGE as -1 next = -1; for (j = i + 1; j < 2 * n; j++) { // Checking for next // greater element if (arr[i] < arr[j]) { next = arr[j]; break; } } // Print the updated NGE cout << next << ", "; } } // Driver Code int main() { // Given array arr[] int arr[] = { 1, 2, 1 }; int N = sizeof(arr) / sizeof(arr[0]); // Function call printNGE(arr, N); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find the NGE static void printNGE(int []A, int n) { // Formation of cicular array int []arr = new int[2 * n]; // Append the given array element twice for(int i = 0; i < 2 * n; i++) arr[i] = A[i % n]; int next; // Iterate for all the // elements of the array for(int i = 0; i < n; i++) { // Initialise NGE as -1 next = -1; for(int j = i + 1; j < 2 * n; j++) { // Checking for next // greater element if (arr[i] < arr[j]) { next = arr[j]; break; } } // Print the updated NGE System.out.print(next + ", "); } } // Driver Code public static void main(String args[]) { // Given array arr[] int []arr = { 1, 2, 1 }; int N = arr.length; // Function call printNGE(arr, N); } } // This code is contributed by Code_Mech |
Python3
# Python3 program for the above approach # Function to find the NGE def printNGE(A, n): # Formation of cicular array arr = [0] * (2 * n) # Append the given array # element twice for i in range(2 * n): arr[i] = A[i % n] # Iterate for all the # elements of the array for i in range(n): # Initialise NGE as -1 next = -1 for j in range(i + 1, 2 * n): # Checking for next # greater element if(arr[i] < arr[j]): next = arr[j] break # Print the updated NGE print(next, end = ", ") # Driver code if __name__ == '__main__': # Given array arr[] arr = [ 1, 2, 1 ] N = len(arr) # Function call printNGE(arr, N) # This code is contributed by Shivam Singh |
C#
// C# program for the above approach using System; class GFG{ // Function to find the NGE static void printNGE(int []A, int n) { // Formation of cicular array int []arr = new int[2 * n]; // Append the given array element twice for(int i = 0; i < 2 * n; i++) arr[i] = A[i % n]; int next; // Iterate for all the // elements of the array for(int i = 0; i < n; i++) { // Initialise NGE as -1 next = -1; for(int j = i + 1; j < 2 * n; j++) { // Checking for next // greater element if (arr[i] < arr[j]) { next = arr[j]; break; } } // Print the updated NGE Console.Write(next + ", "); } } // Driver Code public static void Main() { // Given array arr[] int []arr = { 1, 2, 1 }; int N = arr.Length; // Function call printNGE(arr, N); } } // This code is contributed by Code_Mech |
2, -1, 2,
Time Complexity: O(N2)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find just strictly greater element from first array for each element in second array
- Find closest greater value for every element in array
- Find element in a sorted array whose frequency is greater than or equal to n/2.
- Majority element in a circular array of 0's and 1's
- Closest greater element for every array element from another array
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count of elements in first Array greater than second Array with each element considered only once
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Find Array formed by adding each element of given array with largest element in new array to its left
- Find the element before which all the elements are smaller than it, and after which all are greater
- Elements greater than the previous and next element in an Array
- Count of greater elements for each element in the Array
- Smallest element greater than X not present in the array
- Sum of elements in an array with frequencies greater than or equal to that element
- Closest greater or same value on left side for every element in array
- Adding elements of an array until every element becomes greater than or equal to k
- First strictly greater element in a sorted array in Java
- Rearrange an array such that every odd indexed element is greater than it previous
- Count of distinct index pair (i, j) such that element sum of First Array is greater
- Minimum element whose n-th power is greater than product of an array of size n
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

