• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
April 05, 2022 |85.8K Views
Leaders in an Array | SDE Sheet | Arrays
  Share  26 Likes
Description
Discussion

In this video, we will discuss how to print all the LEADERS in the array. For an element to be considered as a LEADER it has to be greater than all the elements to its right or we can also say that all the elements to the right of the LEADER should be less than the LEADER. 

Also, the element at the rightmost position (the last element) is always a LEADER. 

For example in the array [13,16,4,3,6,2], leaders are 16, 6 and 2. 

We will be using two methods: 

1) Method 1: We will take two loops. The outer loop will run from 0 to size-1, picking the elements one by one from the left. The inner loop will help us compare this picked element with all the elements coming after it (all the elements to it's right). The picked element is considered a LEADER if it is greater than all the elements to its right. 

2) Method 2: We scan all the elements from right to left in an array and keep track of the maximum element. When the value of maximum changes, we print the value. 

Next Video: https://www.geeksforgeeks.org/videos/equilibrium-point-sde-sheet-arrays/ 

Practice Problem: https://practice.geeksforgeeks.org/problems/leaders-in-an-array-1587115620/1 

Article: https://www.geeksforgeeks.org/leaders-in-an-array/ 

SDE Sheet: https://www.geeksforgeeks.org/sde-sheet-a-complete-guide-for-sde-preparation/

Read More