arrays | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

arrays

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 in the array {16, 17, 4, 3, 5, 2}, leaders are 17, 5 and 2.

8th May 2018, 4:23 AM
pavankalyan kopparapu
pavankalyan kopparapu - avatar
1 Answer
0
O(n) solution in C++: https://code.sololearn.com/cKlU8bAi6dug/#cpp n is the length of the array. The idea is to simply find the maximum but starting from the right side of the array. Every time you update your max, just print it.
8th May 2018, 7:20 AM
Ali Zhussupov
Ali Zhussupov - avatar