chore(Java): add next greater element (#833)

* chore(Java): added Next Greater Element

* Update Next_Greater_Element.java file
pull/837/head
Ishantgarg-web 2022-09-11 19:02:36 +05:30 committed by GitHub
parent e1bbc8b302
commit a5ae1dc091
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 0 deletions

View File

@ -75,6 +75,7 @@
- [Celebrity Problem](stacks/celebrity-problem.java) - [Celebrity Problem](stacks/celebrity-problem.java)
- [Sliding Window Maximum](stacks/sliding-window-maximum.java) - [Sliding Window Maximum](stacks/sliding-window-maximum.java)
- [Min Stack](stacks/Min-Stack.java) - [Min Stack](stacks/Min-Stack.java)
- [Next Greater Element](stacks/Next_Greater_Element.java)
## Strings ## Strings

View File

@ -0,0 +1,79 @@
/***
* Problem Statement:
* Given an array arr[] of size N having distinct elements, the task is to find the next greater element for each element of the array in order of their appearance in the array.
* Next greater element means nearest element on right side which is greater than current element,
* if there is no suxh element, then put -1 for that.
*
*/
/***
* Example 1: Input: arr=[1,3,2,4] n=4
Output: 3 4 4 -1
Example 2: Input: arr=[6 8 0 1 3] n=5
Output: 8 -1 1 3 -1
*/
/***
* Time Complexity: O(N)
* Space Complexity: O(N)
*/
import java.util.Scanner;
import java.util.Stack;
public class Next_Greater_Element {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int n=input.nextInt();
long arr[]=new long[n];
for (int i=0;i<n;i++){
arr[i]=input.nextLong();
}
long ans[]=nextLargerElement(arr,n); // functions that print array for calculating next greater element
for (int i=0;i<ans.length;i++){
System.out.print(ans[i]+" ")
}
}
public static long[] nextLargerElement(long[] arr, int n)
{
// Your code here
/****
* Approach: for element at index i,
* there were two posiibilities that there is greater element than this
* at right side of array or not.
* if there is no greater element in right side is indicated by Empty stack
* if there is greater ekement in right side is indicated by stack peek element.
*/
long ans[]=new long[n];
ans[n-1]=-1;
Stack<Long> st=new Stack<>();
st.push(arr[n-1]);
for (int i=n-2;i>=0;i--)
{
if(st.isEmpty())
{
st.push(arr[i]);
ans[i]=-1;
}
else
{
while(st.isEmpty()==false && arr[i]>=st.peek())
{
st.pop();
}
if(st.size()==0){
ans[i]=-1;
}else{
ans[i]=st.peek();
}
st.push(arr[i]);
}
}
return ans;
}
}