fork download
  1. #include <stdio.h>
  2.  
  3. void printLeaders(int arr[], int size) {
  4. int i, j;
  5. for (i = 0; i < size; i++) {
  6. int isLeader = 1; // Assume arr[i] is a leader
  7. for (j = i + 1; j < size; j++) {
  8. if (arr[i] <= arr[j]) {
  9. isLeader = 0; // If any element to the right is greater, arr[i] is not a leader
  10. break;
  11. }
  12. }
  13. if (isLeader) {
  14. printf("%d ", arr[i]); // If arr[i] is a leader, print it
  15. }
  16. }
  17. }
  18.  
  19. /* Driver program to test above function */
  20. int main() {
  21. int T; // Number of test cases
  22. scanf("%d", &T); // Input the number of test cases
  23.  
  24. while (T--) {
  25. int n; // Size of the array
  26. scanf("%d", &n); // Input the size of the array
  27. int arr[n]; // Declare the array
  28.  
  29. // Input the array elements
  30. for (int i = 0; i < n; i++) {
  31. scanf("%d", &arr[i]);
  32. }
  33.  
  34. // Call the function to print leaders
  35. printLeaders(arr, n);
  36. printf("\n");
  37. }
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0s 5296KB
stdin
Standard input is empty
stdout
Standard output is empty