-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathleaders_array.c
More file actions
43 lines (35 loc) · 820 Bytes
/
leaders_array.c
File metadata and controls
43 lines (35 loc) · 820 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
34
35
36
37
38
39
40
41
42
43
/*
Leaders in an array - GeeksForGeeks
Problem Link: https://practice.geeksforgeeks.org/problems/leaders-in-an-array/0
Author: Shyam Kumar
*/
#include <stdio.h>
#include <stdlib.h>
void leadersArray( int *arr, int n ) {
int i, j, k = 0;
int *ans = (int *)malloc( sizeof( int ) * n );
for( i = 0; i < n-1; i++ ) {
for( j = i+1; j < n; j++ ) {
if( arr[j] >= arr[i] )
break;
}
if( j == n )
ans[k++] = arr[i];
}
for( i = 0; i < k; i++ )
printf("%d ",ans[i]);
printf("%d\n", arr[n-1]); //Printing the last element
return;
}
int main( void ) {
int t, n, i, j;
scanf("%d",&t);
for( i = 0; i < t; i++ ) {
scanf("%d",&n);
int *arr = (int *)malloc(sizeof( int ) * n);
for( j = 0; j < n; j++ )
scanf("%d",&arr[j]);
leadersArray( arr, n );
}
return 0;
}