tags:

views:

180

answers:

4

Consider an array element X is one of leaders of the array if the all the elements following that array element is lesser than or equal to the array element X ...then what is the best algorithm to find all the leaders of the array?

Array may have more leaders.. consider the following array [10 9 8 6 ] then 10,9,8 are leaders of the array

+11  A: 

Work from the right hand end of the array, keeping track of the maximum value you have encountered. Every time that maximum increases or is equalled, that element is a leader by your definition. What is more, it stays a leader regardless of what happens further to the left - in other words, every leader you add to your list is a genuine leader, not just a candidate, as you'd have working left to right.

David M
but he wants to find *all* leaders.
Daren Thomas
Yes, and this method does exactly that!
David M
so true. sorry. upvoted!
Daren Thomas
@Daren - no worries, and thanks.
David M
thank u very much.. actually this is a computer science GATE exam question... the options were 1.from left to right pass in linear time 2.from right to left pass in linear time 3.0(n2) 4.divide and conquere method then the answer is option 2
suresh
Uh, this doesn't meet the specification "all following are less OR EQUAL". You need to change "every time that maximum increases" to "every time that maximum is matched or increased".
Svante
"or equal" was edited in to the question after I had answered it, but you are quite right in the light of that - I'll edit my answer accordingly.
David M
A: 

Maintain a list of possible leaders as you work through the array/list. This list is naturally sorted descending. Each new element is a possible new leader. If its bigger than the last found possible leader, then you have to eliminate all leaders smaller than this new possible leader and append it to the now truncated list. Otherwise, just append it to the list of possible leaders.

Python:

def findleaders(array):
    leaders = []
    for element in array:
        if element < leaders[-1]:
            # new possible leader
            leaders.append(element)
        else:
            # remove false possible leaders
            while leaders[-1] < element:
                leaders.pop()
                if not leaders: break #stop when list is empty
            leaders.append(element)
    return leaders
Daren Thomas
A: 

In Haskell it would look like

 leaders [] = [];

leaders (a:as)
          | all (a>) as = a : leaders as
          | otherwise   = leaders as

giving the list of leaders of a list.

  • The list of leaders of an empty list is empty
  • If the first element of a list is a leader, then this is also the first element of the list of leaders.

It should be easy to adapt to arrays.

Ingo
This algorithm is O(n^2) if I understand it correctly. See David M's answer for a simple O(n) algorithm.
j_random_hacker
Yes sure, David's answer is optimal for data structures you can traverse backwards. My algo could be enhanced by dropping all elements (<a) from the fron of the list when a is NOT a leader. Then O^n would be worst case beaviour on descending list only.
Ingo
A: 

Deriving from David M's algorithm

int max = a[LAST_INDEX]
for( int i = a[LAST_INDEX-1] ; i >=0 ; i-- ) {
 if( a[i] > max ) 
 {      
   printf("%d",a[i]);
   max = a[i];
 }
}
suresh