My issue
Solve this code
My code
#include <stdio.h>
void findLeaders(int arr[], int n) {
int max_from_right = arr[n-1]; // Rightmost element is always a leader
printf("%d ", max_from_right); // Print the rightmost element as a leader
// Traverse the array from the second last element to the first element
for (int i = n - 2; i >= 0; i--) {
if (arr[i] > max_from_right) {
max_from_right = arr[i];
printf("%d ", arr[i]); // Print the current element if it's a leader
}
}
}
int main() {
int n;
scanf("%d", &n); // Read the number of elements
int arr[n];
// Read the array elements
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Find and print the leaders
findLeaders(arr, n);
return 0;
}
Learning course: Data structures & Algorithms lab
Problem Link: Leader of an Array in Data structures & Algorithms lab