Difficulty: easy, Asked-in: Microsoft, Amazon, Adobe, Goldman Sachs
Key takeaway: An excellent problem to learn problem-solving using a single scan.
Let's understand the problem
Given an integer array X[] of size n, write a program to find all the leaders in the array X[]. An element is a leader if it is strictly greater than all the elements to its right side.
- The last element of an array is a leader by default.
- The largest element of an array is also a leader by default.
- Suppose all the array elements are unique.
- Ordering in the output doesn’t matter.
Example 1
Input: X[] = [16, 17, 4, 3, 5, 2], Output: [17, 5, 2]
Explanation: Element 2 is the rightmost element, so it is a leader by default. 17 and 5 are also leader elements because they are greater than all the elements on their right side.
Example 2
Input: X[] = [6, 5, 4, 3, 2, 1], Output: [6, 5, 4, 3, 2, 1]
Explanation: All elements are leaders because they are greater than all the elements on their right side. Element 1 is the rightmost element, so it is a leader by default.
Example 3
Input: X[] = [1, 2, 3, 4, 5, 6], Output: [6]
Explanation: Element 6 is the rightmost element, which is a leader by default. Otherwise, all elements are less than all on their right side.
Discussed solution approaches
- Brute force solution using two nested loops
- Efficient solution using a single scan from the right
Brute force solution using two nested loops
Solution idea
Based on the definition, leaders are the elements in an array that are greater than all the elements on their right side. So, one basic idea would be to explore each element X[i] and check whether all the elements on the right side of the array are less than X[i] or not. If yes, we add it to the output. Otherwise, we ignore X[i] and move to the next element. To implement this, we can run two nested loops:
- The outer loop from i = 0 to n - 1 explores each element X[i] from left to right.
- The inner loop from j = i + 1 to n - 1 compares X[i] with all the elements to its right side. If X[i] is greater than all the elements to its right side, X[i] is a leader element, and we add it to the output.
Solution pseudocode
findLeaders(X, n)
leaders = empty list
// Check each element to see whether it is greater than or equal to
// every element to its right
for i from 0 to n - 1
j = i + 1
// Stop if a larger element is found to the right
while j < n
if X[i] < X[j]
break
j = j + 1
// If no larger element was found, X[i] is a leader
if j == n
leaders.push_back(X[i])
return leadersSolution analysis
We are running two nested loops and performing O(1) operations at each iteration. The total number of loop iterations in the worst-case scenario = (n - 1) + (n - 2) + ... + 1 = n(n - 1)/2. So the time complexity in the worst-case scenario = O(1) * n(n - 1)/2 = O(n^2). The critical question is: what would be the scenarios for the worst and best-case inputs? Think! The space complexity is O(1) because the leaders[] array is part of the output given in the problem.
Efficient solution using a single scan from right side
Solution idea
Now, the critical question is: can we solve this problem in O(n) time? Let's think!
An element X[i] is a leader if it is greater than the maximum element to its right. One idea is to scan the elements from right to left and keep track of the maximum value seen so far in a variable called maxFromRight. Whenever we find X[i] > maxFromRight, X[i] is a leader because it is greater than every element to its right. In other words, whenever maxFromRight changes, we have found a leader.
Note: The last element of the array is a leader by default, so we initialise maxFromRight to X[n - 1].
Solution pseudocode
findLeaders(X, n)
leaders = empty list
// The last element is always a leader
maxFromRight = X[n - 1]
leaders.push_back(maxFromRight)
// Scan the array from right to left
for i from n - 2 down to 0
// X[i] is a leader if it is greater than every element to its right
if X[i] > maxFromRight
maxFromRight = X[i]
leaders.push_back(maxFromRight)
return leadersSolution analysis
We are running a single loop of size and performing an O(1) operation at each iteration. So time complexity = O(n) * O(1) = O(n). Space complexity = O(1).
Critical ideas to think!
- How can we modify the above solutions to store the output in a stable order or in the same order as they are present in the input?
- How can we modify the above code to handle the scenario of duplicate elements?
- Can we think of solving this problem using recursion? If yes, what would be the time and space complexity?
- Can we think of solving this problem using a stack? If yes, what would be the time and space complexity?
Comparison of time and space complexities
- Using two nested loops: Time = O(n^2), Space = O(1)
- Using a single scan: Time = O(n), Space = O(1)
Suggested coding problems to explore
- Number of buildings facing the sun
- Find next greater elements in an array
- Find equilibrium index of an array
- Move zeroes to the end of an array
- The minimum and maximum value in an array
Please write in the message below if you find anything incorrect, or you want to share more insight. Enjoy learning, Enjoy algorithms!
