#!/usr/bin/env python # coding: utf-8 # # Leaders # 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. # In[1]: def getLeaders(arr): big = arr[len(arr) - 1] yield big for i in range(len(arr) - 2, -1, -1): if arr[i] > big: big = arr[i] yield big # In[2]: arr = [16, 17, 4, 3, 5, 2] arr # In[3]: list(getLeaders(arr))