-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNextGreat.java
43 lines (41 loc) · 979 Bytes
/
NextGreat.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
import java.util.*;
public class NextGreat {
int queue[];
int front,rear;
NextGreat(int n){
queue=new int[n];
front=0;
rear=-1;
}
int nextGreat(int x) {
while(front!=rear+1) {
if(queue[front]>queue[x])
return front;
dequeue();
}
return -1;
}
void dequeue() {
front++;
}
void enqueue(int d) {
queue[++rear]=d;
}
public static void main(String args[]) {
System.out.println("Enter array size");
int n=new Scanner(System.in).nextInt();
NextGreat ob=new NextGreat(n);
System.out.println("Enter elements to the array");
for(int i=0;i<n;i++)
ob.enqueue(new Scanner(System.in).nextInt());
for(int i=0;i<n-1;i++) {
ob.front=i;
int x=ob.nextGreat(i);
if(x!=-1)
System.out.println("Element= "+ob.queue[i]+"\nNext Greatest= "+ob.queue[x]);
else
System.out.println("No next greatest for "+ob.queue[i]);
}
System.out.println("Last element has no greatest next");
}
}