7

Leaders in an Array | SDE Sheet | Arrays

 2 years ago
source link: https://www.geeksforgeeks.org/videos/leaders-in-an-array-gatuto/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Leaders in an Array
Leaders in an Array | SDE Sheet | Arrays
Video Player is loading.
Loaded: 0%
Remaining Time -0:00
  • 70 Views
  • 05/04/2022

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.

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/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK