Created
April 24, 2014 19:01
-
-
Save gabhi/11265636 to your computer and use it in GitHub Desktop.
Leaders in Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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