Загрузка страницы

Leaders in an array

Given an array of integers, print the leaders in the array. A leader is an element which is larger than all the elements in the array to its right.

Algorithm:
* Start iterating over the input array starting from the rightmost element keeping track of largest element (lets call it as currentLeader).
* If a larger element is found, then print it and set currentLeader to the current element.
* Note that the last element is a leader since there is no element to its right.

The algorithm can be visualized by considering array elements as towers and printing towers which are visible if seen through right view of the array.
Please note that leaders will be seen in sorted order when seen from right(right view of the towers).

Order of the Algorithm:
Time Complexity: O(n)
Space Complexity: O(1)

Code and Algorithm Visualization:
http://www.ideserve.co.in/learn/leaders-in-an-array

Website: http://www.ideserve.co.in

Facebook: https://www.facebook.com/IDeserve.co.in

Видео Leaders in an array канала IDeserve
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
27 февраля 2016 г. 3:11:16
00:03:36
Яндекс.Метрика