-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplaceGrtstEl.java
More file actions
50 lines (37 loc) · 783 Bytes
/
ReplaceGrtstEl.java
File metadata and controls
50 lines (37 loc) · 783 Bytes
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
// Replace every element with the greatest element on right side
import java.util.*;
public class ReplaceGrtstEl
{
public static void main(String args[])
{
int i,max,temp,n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of Array: ");
n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter the Element of Array: ");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
temp=a[n-1];
max=a[n-1];
a[n-1]=-1;
int temp1=a[0];
int temp2=a[1];
for(i=n-2;i>=0;i--)
{
temp=a[i];
a[i]=max;
if(max<temp)
{
max=temp;
}
}
System.out.println(" New Array: ");
if(temp1>temp2)
System.out.print(""+temp1+" ");
for(i=0;i<n;i++)
System.out.print(""+a[i]+" ");
}
}