Given an array arr[] of integers, determine the Next Greater Element (NGE) for every element in the array, maintaining the order of appearance.
- The Next Greater Element for an element x is defined as the first element to the right of x in the array that is strictly greater than x.
- If no such element exists for an element, its Next Greater Element is -1.
Examples:
Input: arr[] = [1, 3, 2, 4]
Output: [3, 4, 4, -1]
Explanation: The next larger element to 1 is 3, 3 is 4, 2 is 4 and for 4, since it doesn't exist, it is -1.Input: arr[] = [6, 8, 0, 1, 3]
Output: [8, -1, 1, 3, -1]
Explanation: The next larger element to 6 is 8, for 8 there is no larger elements hence it is -1, for 0 it is 1 , for 1 it is 3 and then for 3 there is no larger element on right and hence -1.
Table of Content
[Naive Approach] Using Nested Loops - O(n2) Time and O(1) Space
The idea is to look at all the elements to its right and check if there's a larger element for each element in the array. If we find one, we store it as the next greater element. If no greater element is found, we store -1. This is done using two nested loops: the outer loop goes through each element, and the inner loop searches the rest of the array for a greater element.
#include <iostream>
#include <vector>
using namespace std;
vector<int> nextLargerElement(vector<int> &arr) {
int n = arr.size();
vector<int> res(n, -1);
// iterate through each element in the array
for (int i = 0; i < n; i++) {
// check for the next greater element
// in the rest of the array
for (int j = i + 1; j < n; j++) {
if (arr[j] > arr[i]) {
res[i] = arr[j];
break;
}
}
}
return res;
}
int main() {
vector<int> arr = {6, 8, 0, 1, 3};
vector<int> res = nextLargerElement(arr);
for (int x : res) {
cout << x << " ";
}
return 0;
}
import java.util.ArrayList;
class GfG {
static ArrayList<Integer> nextLargerElement(int[] arr) {
int n = arr.length;
ArrayList<Integer> res = new ArrayList<>();
// initialize res with -1 for all elements
for (int i = 0; i < n; i++) {
res.add(-1);
}
// iterate through each element in the array
for (int i = 0; i < n; i++) {
// check for the next greater element
// in the rest of the array
for (int j = i + 1; j < n; j++) {
if (arr[j] > arr[i]) {
res.set(i, arr[j]);
break;
}
}
}
return res;
}
public static void main(String[] args) {
int[] arr = { 6, 8, 0, 1, 3 };
ArrayList<Integer> res = nextLargerElement(arr);
for (int x : res) {
System.out.print(x + " ");
}
}
}
def nextLargerElement(arr):
n = len(arr)
res = [-1] * n
# Iterate through each element in the array
for i in range(n):
# Check for the next greater element
# in the rest of the array
for j in range(i + 1, n):
if arr[j] > arr[i]:
res[i] = arr[j]
break
return res
if __name__ == "__main__":
arr = [6, 8, 0, 1, 3]
res = nextLargerElement(arr)
print(" ".join(map(str, res)))
using System;
using System.Collections.Generic;
class GfG {
static List<int> nextLargerElement(int[] arr) {
int n = arr.Length;
List<int> res = new List<int>(new int[n]);
// Initialize res with -1 for all elements
for (int i = 0; i < n; i++) {
res[i] = -1;
}
// Iterate through each element in the array
for (int i = 0; i < n; i++) {
// Check for the next greater element
// in the rest of the array
for (int j = i + 1; j < n; j++) {
if (arr[j] > arr[i]) {
res[i] = arr[j];
break;
}
}
}
return res;
}
static void Main(string[] args) {
int[] arr = { 6, 8, 0, 1, 3 };
List<int> res = nextLargerElement(arr);
foreach(int x in res) { Console.Write(x + " "); }
}
}
function nextLargerElement(arr) {
let