Skip to content

Commit 14effb8

Browse files
authored
Create ArrayLeaders.java
1 parent 1486893 commit 14effb8

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Given an integer array A of size n. Find and print all the leaders present in the input array.
3+
An array element A[i] is called Leader, if all the elements following it (i.e. present at its right) are less than or equal to A[i].
4+
Print all the leader elements separated by space and in the same order they are present in the input array.
5+
6+
Input Format :
7+
Line 1 : Integer n, size of array
8+
Line 2 : Array A elements (separated by space)
9+
10+
Output Format :
11+
leaders of array (separated by space)
12+
13+
Constraints :
14+
1 <= n <= 10^6
15+
16+
Sample Input 1 :
17+
6
18+
3 12 34 2 0 -1
19+
Sample Output 1 :
20+
34 2 0 -1
21+
22+
Sample Input 2 :
23+
5
24+
13 17 5 4 6
25+
Sample Output 2 :
26+
17 6
27+
*/
28+
29+
public class ArrayLeaders {
30+
31+
public static void leaders(int[] input) {
32+
for (int i=0;i<input.length;i++)
33+
{
34+
boolean check=true;
35+
for (int j=i+1;j<input.length;j++)
36+
{
37+
if (input[j]>input[i])
38+
{
39+
check=false;
40+
break;
41+
}
42+
}
43+
if (check==true)
44+
{
45+
System.out.print(input[i] + " ");
46+
}
47+
}
48+
}
49+
50+
}
51+
52+

0 commit comments

Comments
 (0)