forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeadersInArray.java
45 lines (37 loc) · 1.19 KB
/
LeadersInArray.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
44
45
package com.rampatra.arrays;
import java.util.Arrays;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 7/29/15
* @time: 12:06 PM
*/
public class LeadersInArray {
/**
* Returns an array containing all leaders present in {@param a}.
* An element is a LEADER if its greater than all elements to
* the right of it in the array.
*
* @param a
* @return
*/
public static int[] getAllLeaders(int[] a) {
int i = a.length - 2, j = 0;
int[] leaders = new int[a.length];
// rightmost element is always a leader
leaders[0] = a[a.length - 1];
for (; i >= 0; i--) {
if (a[i] > leaders[j]) {
leaders[++j] = a[i];
}
}
// omit the extra space which aren't filled with leaders
return Arrays.copyOfRange(leaders, 0, j + 1);
}
public static void main(String[] args) {
System.out.println(Arrays.toString(getAllLeaders(new int[]{16, 17, 4, 3, 5, 2})));
System.out.println(Arrays.toString(getAllLeaders(new int[]{16, 1, 4, 3, 5, 12})));
System.out.println(Arrays.toString(getAllLeaders(new int[]{16, 15, 14, 13, 12, 10})));
}
}