Skip to content

Instantly share code, notes, and snippets.

@gabhi
Created April 24, 2014 19:01
Show Gist options
  • Save gabhi/11265636 to your computer and use it in GitHub Desktop.
Save gabhi/11265636 to your computer and use it in GitHub Desktop.
Leaders in Array
/**
* Write a program to print all the LEADERS in the array.
* An element is leader if it is greater than all the elements to its right side.
* And the rightmost element is always a leader. For example int the array {16, 17, 4, 3, 5, 2}, leaders are 17, 5 and 2.
*/
void printLeaders(int arr[], int size)
{
int max_from_right = arr[size-1];
int i;
/* Rightmost element is always leader */
printf("%d ", max_from_right);
for(i = size-2; i >= 0; i--)
{
if(max_from_right < arr[i])
{
printf("%d ", arr[i]);
max_from_right = arr[i];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment