Find Leader in the Given ArrayIn this tutorial, we will write the Python code to find the leader elements in the given array. A leader refers to an element in an array that is greater than or equal to all the elements to its right side. In other words, an element is considered a leader if it is larger than or equal to every element that comes after it in the array. Let's understand the following example - Example - Explanation - 17 is greater than all elements to its right (4, 3, 5, 2). 5 is greater than all elements to its right (2). 2 is the rightmost element, so it is always considered a leader. So, the leaders in this array are [17, 5, 2] Example - Explanation - 4 and 0, as they are greater than or equal to all the elements to their right side. Let's understand the solution of the above problem. SolutionWe will solve this problem using the various approaches. Naïve Approach
Let's understand the following example - Example - Output: Leaders in the array: [4, 0] Explanation - We define a function find_leaders() that takes an array arr as input. We initialize empty list leaders to store the leaders found in the array. We traverse the array from left to right using two nested loops. The outer loop iterates through elements from the first to the second-to-last element, and the inner loop compares each element with elements to its right. We use a Boolean variable is_leader to assume that the current element is a leader initially. Inside the inner loop, if we find any element greater than the current element, we set is_leader to False and break out of the loop because the current element cannot be a leader. If is_leader is still true after checking all elements to the right, it means the current element is a leader, so we append it to the leaders list. Finally, we append the rightmost element (last element) to the leaders list because it is always a leader. The time complexity of the naive approach to find leaders in an array is O(n^2), and the space complexity is O(1). Approach - 2: Find Leader by finding suffix maximumFinding the suffix maximum in an array means identifying the maximum value among the elements to the right of a specific element in the array, including the element itself. Below is the approach of suffix maximum. Start by initializing a variable to keep track of the maximum value encountered while traversing the array from right to left. Begin iterating through the array from right to left. At each step, compare the current element with the maximum value encountered so far (initialized in step 1). If the current element is greater than the maximum value, update the maximum value to be the current element because it represents the maximum value to the right of the current element. Continue this process for all elements in the array, effectively finding the maximum value in the suffix (elements to the right) for each element. As you traverse the array, record or print the maximum value encountered for each element. These recorded values represent the suffix maximums for the corresponding elements in the original array. Let's understand the following example - Example - Output: Leaders in the array: [17, 5, 2] Explanation - We define a function find_leaders that takes an array arr as input. We find the length of the array n and initialize empty list leaders to store the leader elements. We start by initializing the max_right variable with the rightmost element of the array, which is arr[n - 1]. The rightmost element is always a leader, so we add it to the leaders list. We then iterate through the array from the second-to-last element (index n - 2) to the first element (index 0) in reverse order (from right to left). Inside the loop, we compare each element with the current max_right. If the current element is greater than or equal to max_right, it becomes the new leader, and we update max_right accordingly. We add each leader element to the leaders list as we encounter them. After the loop, we reverse the leaders list to obtain the elements in their original order. Finally, we return the leaders list containing all the leader elements in the array. In the example usage, we call the find_leaders function with an example array and print the leaders found in the array. Approach - 3: Stack-Based ApproachThe previous approach provided a linear time complexity, but it didn't maintain the order of elements as they appear in the input array. To preserve the original order of elements while finding leaders, we can employ a stack data structure. Here are the revised steps for identifying leaders in an array using a stack-based approach:
Let's understand the following example. Example - Output: 4 0 Explanation - We define a function find_leaders that takes an array arr as input. We calculate the length of the input array n and initialize an empty stack stack to store the leaders. We initialize max_element as the last element of the input array because the rightmost element is always a leader. We push this maximum element onto the stack to start. We then iterate through the array from the second-to-last element (index n-2) to the first element (index 0) in reverse order using a for loop. Inside the loop, we compare the current element arr[i] with the max_element. If the current element is greater than or equal to the max_element, it is considered a leader, so we push it onto the stack. We also update max_element to the current element if it's greater. After iterating through the array, we have identified and stored the leaders in the stack. Finally, we print the leaders in the same order as they appear in the input array by popping elements from the stack and printing them. ConclusionIn this tutorial, we learned how to find leader elements in an array, which are elements greater than or equal to all elements to their right. We explored three different approaches such as naïve approach which involved iterating through the array and comparing each element with those to its right. It has a time complexity of O(n2). Finding leaders by finding suffix maximum where we optimized the process by finding the maximum element to the right of each element. It achieved a linear time complexity of O(n) and preserved the order of elements. And Stack-Based Approach which is used to to maintain the original order of elements, we used a stack. Starting from the rightmost element, we iterated through the array in reverse, pushing leaders onto the stack. This method also had a time complexity of O(n). |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India