-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleaders_of_array.java
More file actions
39 lines (36 loc) · 1.16 KB
/
Copy pathleaders_of_array.java
File metadata and controls
39 lines (36 loc) · 1.16 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
/* Given an array A of positive integers. Your task is to find the leaders in the array.
An element of array is leader if it is greater than or equal to all the elements to its right side.
The rightmost element is always a leader. */
import java.util.*;
public class leaders_of_array
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter total number of elements in the array n: ");
int n = sc.nextInt();
System.out.print("Enter " + n + " elements of the array: ");
int[] a = new int[n];
for(int i=0; i<n; i++)
{
a[i] = sc.nextInt();
}
System.out.println("Leaders of the array: " + leaders(a,n));
sc.close();
}
static ArrayList<Integer> leaders(int arr[], int n)
{
ArrayList<Integer> list = new ArrayList<>();
list.add(arr[n-1]);
int max = arr[n-1];
for(int i=n-2; i>=0; i--)
{
if(arr[i]>=max)
{
list.add(arr[i]);
max=arr[i];
}
}
Collections.reverse(list);
return list;
}
}