Given an array of distinct elements, find previous greater element for every element. If previous greater element does not exist, print -1.
Examples:
Input : arr[] = {10, 4, 2, 20, 40, 12, 30}
Output : -1, 10, 4, -1, -1, 40, 40
Input : arr[] = {10, 20, 30, 40}
Output : -1, -1, -1, -1
Input : arr[] = {40, 30, 20, 10}
Output : -1, 40, 30, 20
Expected time complexity : O(n)
A simple solution is to run two nested loops. The outer loop picks an element one by one. The inner loop, find the previous element that is greater.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void prevGreater( int arr[], int n)
{
cout << "-1, " ;
for ( int i = 1; i < n; i++) {
int j;
for (j = i-1; j >= 0; j--) {
if (arr[i] < arr[j]) {
cout << arr[j] << ", " ;
break ;
}
}
if (j == -1)
cout << "-1, " ;
}
}
int main()
{
int arr[] = { 10, 4, 2, 20, 40, 12, 30 };
int n = sizeof (arr) / sizeof (arr[0]);
prevGreater(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG
{
static void prevGreater( int arr[],
int n)
{
System.out.print( "-1, " );
for ( int i = 1 ; i < n; i++)
{
int j;
for (j = i- 1 ; j >= 0 ; j--)
{
if (arr[i] < arr[j])
{
System.out.print(arr[j] + ", " );
break ;
}
}
if (j == - 1 )
System.out.print( "-1, " );
}
}
public static void main(String[] args)
{
int arr[] = { 10 , 4 , 2 , 20 , 40 , 12 , 30 };
int n = arr.length;
prevGreater(arr, n);
}
}
|
Python 3
def prevGreater(arr, n) :
print ( "-1" ,end = ", " )
for i in range ( 1 , n) :
flag = 0
for j in range (i - 1 , - 1 , - 1 ) :
if arr[i] < arr[j] :
print (arr[j],end = ", " )
flag = 1
break
if j = = 0 and flag = = 0 :
print ( "-1" ,end = ", " )
if __name__ = = "__main__" :
arr = [ 10 , 4 , 2 , 20 , 40 , 12 , 30 ]
n = len (arr)
prevGreater(arr, n)
|
C#
using System;
class GFG
{
static void prevGreater( int [] arr,
int n)
{
Console.Write( "-1, " );
for ( int i = 1; i < n; i++)
{
int j;
for (j = i-1; j >= 0; j--)
{
if (arr[i] < arr[j])
{
Console.Write(arr[j] + ", " );
break ;
}
}
if (j == -1)
Console.Write( "-1, " );
}
}
public static void Main()
{
int [] arr = {10, 4, 2, 20, 40, 12, 30};
int n = arr.Length;
prevGreater(arr, n);
}
}
|
PHP
<?php
function prevGreater(& $arr , $n )
{
echo ( "-1, " );
for ( $i = 1; $i < $n ; $i ++)
{
for ( $j = $i -1; $j >= 0; $j --)
{
if ( $arr [ $i ] < $arr [ $j ])
{
echo ( $arr [ $j ]);
echo ( ", " );
break ;
}
}
if ( $j == -1)
echo ( "-1, " );
}
}
$arr = array (10, 4, 2, 20, 40, 12, 30);
$n = sizeof( $arr ) ;
prevGreater( $arr , $n );
?>
|
Javascript
<script>
function prevGreater(arr,n)
{
document.write( "-1, " );
for (let i = 1; i < n; i++)
{
let j;
for (j = i-1; j >= 0; j--)
{
if (arr[i] < arr[j])
{
document.write(arr[j] + ", " );
break ;
}
}
if (j == -1)
document.write( "-1, " );
}
}
let arr=[10, 4, 2, 20, 40, 12, 30];
let n = arr.length;
prevGreater(arr, n);
</script>
|
Output
-1, 10, 4, -1, -1, 40, 40,
An efficient solution is to use stack data structure. If we take a closer look, we can notice that this problem is a variation of stock span problem. We maintain previous greater element in a stack.
C++
#include <bits/stdc++.h>
using namespace std;
void prevGreater( int arr[], int n)
{
stack< int > s;
s.push(arr[0]);
cout << "-1, " ;
for ( int i = 1; i < n; i++) {
while (s.empty() == false && s.top() < arr[i])
s.pop();
s.empty() ? cout << "-1, " : cout << s.top() << ", " ;
s.push(arr[i]);
}
}
int main()
{
int arr[] = { 10, 4, 2, 20, 40, 12, 30 };
int n = sizeof (arr) / sizeof (arr[0]);
prevGreater(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG
{
static void prevGreater( int arr[],
int n)
{
Stack<Integer> s = new Stack<Integer>();
s.push(arr[ 0 ]);
System.out.print( "-1, " );
for ( int i = 1 ; i < n; i++)
{
while (s.empty() == false &&
s.peek() < arr[i])
s.pop();
if (s.empty() == true )
System.out.print( "-1, " );
else
System.out.print(s.peek() + ", " );
s.push(arr[i]);
}
}
public static void main(String[] args)
{
int arr[] = { 10 , 4 , 2 , 20 , 40 , 12 , 30 };
int n = arr.length;
prevGreater(arr, n);
}
}
|
Python3
import math as mt
def prevGreater(arr, n):
s = list ();
s.append(arr[ 0 ])
print ( "-1, " , end = "")
for i in range ( 1 , n):
while ( len (s) > 0 and s[ - 1 ] < arr[i]):
s.pop()
if len (s) = = 0 :
print ( "-1, " , end = "")
else :
print (s[ - 1 ], ", " , end = "")
s.append(arr[i])
arr = [ 10 , 4 , 2 , 20 , 40 , 12 , 30 ]
n = len (arr)
prevGreater(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void prevGreater( int []arr,
int n)
{
Stack< int > s = new Stack< int >();
s.Push(arr[0]);
Console.Write( "-1, " );
for ( int i = 1; i < n; i++)
{
while (s.Count != 0 &&
s.Peek() < arr[i])
s.Pop();
if (s.Count == 0)
Console.Write( "-1, " );
else
Console.Write(s.Peek() + ", " );
s.Push(arr[i]);
}
}
public static void Main(String[] args)
{
int []arr = { 10, 4, 2, 20, 40, 12, 30 };
int n = arr.Length;
prevGreater(arr, n);
}
}
|
Javascript
<script>
function prevGreater(arr,n)
{
let s = [];
s.push(arr[0]);
document.write( "-1, " );
for (let i = 1; i < n; i++)
{
while (s.length!=0 &&
s[s.length-1] < arr[i])
s.pop();
if (s.length==0)
document.write( "-1, " );
else
document.write(s[s.length-1] + ", " );
s.push(arr[i]);
}
}
let arr=[10, 4, 2, 20, 40, 12, 30];
let n = arr.length;
prevGreater(arr, n);
</script>
|
Output
-1, 10, 4, -1, -1, 40, 40,
Complexity Analysis:
- Time Complexity: O(n). It seems more than O(n) at first look. If we take a closer look, we can observe that every element of array is added and removed from stack at most once. So there are total 2n operations at most. Assuming that a stack operation takes O(1) time, we can say that the time complexity is O(n).
- Auxiliary Space: O(n) in worst case when all elements are sorted in decreasing order.
Last Updated :
26 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...