• edited by
30,712 views
81 81 votes

An element in an array $X$ is called a leader if it is greater than all elements to the right of it in $X$. The best algorithm to find all leaders in an array 

  1. solves it in linear time using a left to right pass of the array
  2. solves it in linear time using a right to left pass of the array
  3. solves it using divide and conquer in time $\Theta (n\log n)$
  4. solves it in time $\Theta( n^2)$

13 Answers

Best answer
95 95 votes

Option B. We can move from right to left, while keeping a note of the maximum element so far (let's call it current_max).

  •  Starting from the rightmost element, we initialize our current_max with it, since the rightmost element will always be a leader.
  •  Moving from right to left, if an element $x$ is greater than our current_max, then $x$ is also a leader. Add this element to list of leaders (or simply print it). Set current_max to $x$ and carry-on leftward.

Time Complexity would be $\Theta(n)$.

• edited by
4 4 votes
I think sorting the array using divide and conquer will be a better a idea so option c is correct.
• edited by
2 flags:
✌ Low quality (js__)
✌ Low quality (Atharva_Shede)
1 1 vote
\\Ref: https://www.geeksforgeeks.org/leaders-in-an-array/
1.#include <iostream> 
2.using namespace std; 
3.void printLeaders(int arr[], int size) 
4.{   int max_from_right = arr[size-1]; 
5.    /* Rightmost element is always leader */
6.	cout << max_from_right << " "; 
7.	for (int i = size-2; i >= 0; i--) 
8.{       if (max_from_right <= arr[i]) 
9.		{	max_from_right = arr[i]; 
10.			cout << max_from_right << " "; }}} 
11.int main() 
12.{   int arr[] = {16, 17, 4, 3, 5, 2}; 
13.	int n = sizeof(arr)/sizeof(arr[0]); 
14.	printLeaders(arr, n); 
15.	return 0; }	 
    Till 5th line.6thstep o/p$=2$ 
for loop 1st time. o/p$=2\ 5$
   for loop 2nd,3rd,4th time. o/p$=2\ 5\ 17$

for loop will execute 5th and 6th time also without making any changes and after that, the function will return. So $2,5,17$ are the leaders displayed in the o/p. Answer : B

0 0 votes

I think, that this problem is only reverse sorting or putting elements in descending order,

In order to do that start scanning from right.

So. ANSWER is B.

Answer:
Position:
Show:

Related questions

95 95 votes
14 answers 14 answers
50.1k
50.1k views
Rucha Shelke asked Sep 26, 2014
50,083 views
Given two arrays of numbers $a_{1},...,a_{n}$ and $b_{1},...,b_{n}$ where each number is $0$ or $1$, the fastest algorithm to find the largest span $(i, j)$ such that $ a...
106 106 votes
9 answers 9 answers
42.5k
42.5k views
Rucha Shelke asked Sep 17, 2014
42,537 views
Consider the following log sequence of two transactions on a bank account, with initial balance $12000,$ that transfer $2000$ to a mortgage payment and then apply a $5\%$...
57 57 votes
3 answers 3 answers
17.1k
17.1k views
Rucha Shelke asked Sep 26, 2014
17,120 views
Consider the following C-function in which $a[n]$ and $b[m]$ are two sorted integer arrays and $c[n+m]$ be another integer array,void xyz(int a[], int b [], int c []){ in...
25 25 votes
4 answers 4 answers
8.8k
8.8k views
Rucha Shelke asked Sep 26, 2014
8,800 views
A set $X$ can be represented by an array $x[n]$ as follows: $x\left [ i \right ]=\begin {cases} 1 & \text{if } i \in X \\ 0 & \text{otherwise} \end{cases}$Consider the ...