-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeaderInArray.java
More file actions
33 lines (25 loc) · 790 Bytes
/
Copy pathLeaderInArray.java
File metadata and controls
33 lines (25 loc) · 790 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
package array;
import java.util.Arrays;
public class LeaderInArray {
static void largestElementInArray(int[] a) {
int largest = Integer.MIN_VALUE; //give the minimum value
int b[] = new int[a.length];
int j = 0;
for (int i = a.length - 1; i >= 0; i--) {
if (a[i] > largest) {
largest = a[i];
// System.out.print(a[i]+" "); for printing 3,4,6,7
// b[j++]=a[i];
b[j] = a[i];
j++;
}
}
for (j = j - 1; j >= 0; j--) {
System.out.println("index j: " + j + " =>>> value: " + b[j]);
}
}
public static void main(String[] args) {
int[] a = {2, 7, 6, 4, 1, 3};
largestElementInArray(a);
}
}