-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace_next_greatest_value.java
More file actions
49 lines (46 loc) · 1.01 KB
/
replace_next_greatest_value.java
File metadata and controls
49 lines (46 loc) · 1.01 KB
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
package zoho;
import java.util.*;
/**
* Input:
7
1 9 7 36 91 101 46
* output:
7 36 36 46 101 -1 -1
*
* * Input:
5
2 -1 0 -1 3
* output:
3 0 3 3 -1
*/
public class replace_next_greatest_value {
public static void main(String[] args)
{
Scanner c = new Scanner(System.in);
int n = c.nextInt();
int[] a = new int[n];
ArrayList<Integer> t = new ArrayList<>();
for(int i=0;i<n;i++)
a[i]=c.nextInt();
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
t.add(a[j]);
Collections.sort(t);
boolean f=true;
for(int j:t)
{
if(j>a[i])
{
a[i]=j;
f=false;
break;
}
}
if(f) a[i]=-1;
t.clear();
}
for(int i:a)
System.out.print(i+" ");
}
}