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
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.