-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leader_in_array.txt
57 lines (48 loc) · 1.5 KB
/
Leader_in_array.txt
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
51
52
53
54
55
56
57
Leaders in array
Send Feedback
Given an integer array A of size n. Find and print all the leaders present in the input array. 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].
Print all the leader elements separated by space and in the same order they are present in the input array.
Input Format :
Line 1 : Integer n, size of array
Line 2 : Array A elements (separated by space)
Output Format :
leaders of array (separated by space)
Constraints :
1 <= n <= 10^6
Sample Input 1 :
6
3 12 34 2 0 -1
Sample Output 1 :
34 2 0 -1
Sample Input 2 :
5
13 17 5 4 6
Sample Output 2 :
17 6
public class Solution {
public static void leaders(int[] input) {
/* Your class should be named Solution
* Don't write main().
* Don't read input, it is passed as function argument.
* Print output and don't return it.
* Taking input is handled automatically.
*/
int n = input.length;
boolean flag = true;
for(int i=0; i<n-1; i++){
for(int j=i+1; j<n; j++){
if(input[j]>input[i]){
flag = false;
break;
}else {
flag = true;
}
}
if(flag==true){
System.out.print(input[i] + " ");
flag=false;
}
}
System.out.print(input[n-1]);
}
}