-
Notifications
You must be signed in to change notification settings - Fork 1k
/
nextGreaterElementLeft.java
63 lines (55 loc) · 1.43 KB
/
nextGreaterElementLeft.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Given an array of numbers find the next greater element to the left of each element.
ALGORITHM:
*Using Stack class in Java, the problem can be solved in O(n) complexity
*/
import java.lang.Scanner;
import java.util.Stack;
public class NextGreaterElement
{
public static void nextGreaterElementToLeft(int a[],int n)
{
Stack<Integer> st=new Stack<>();
int v[]=new int[n];
for(int i=0;i<n;i++)
{
if(st.empty())
v[i]=-1;
else if(st.empty()!=true && st.top()>a[i])
v[i]=st.top();
else if(st.empty()!=true && st.top()<=a[i])
{
while(st.empty()!=true && st.top()<=a[i])
st.pop();
if(st.empty())
v[i]=-1;
else if(st.top()>a[i])
v[i]=st.top();
}
}
for(int i=0;i<n;i++)
System.out.print(v[i]+" ");
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the size of the array: ");
int n=s.nextInt(System.in);
System.out.println("Enter the elements: ");
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=s.nextInt();
nextGreaterElementToLeft(a,n);
}
}
/*
Time Complexity: O(n)
Space Complexity: O(n)
Input:
Enter the size of the array:
5
Enter the elements:
9 3 7 1 0
Output:
-1 9 9 7 1
*/