Skip to main content
Code Review

Return to Question

added 3 characters in body
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

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 \$Θ(nlogn)\$\$Θ(n \log n)\$
  4. Solves it in time \$Θ(n^2)\$

At the start the right most element will always be a leader. If an element is greater than our current_max, it will a leader. Add this element to leaders. Set current_max to this element and carry on leftward. Time complexity would be \$O(n)\$.

Can you give an algorithm for another way or better way to find leader in an array?

#include <iostream>
using namespace std;
/* C++ Function to print leaders in an array */
void printLeaders(int arr[], int size)
{
 int max_from_right = arr[size-1];
 /* Rightmost element is always leader */
 cout << max_from_right << " ";
 for (int i = size-2; i >= 0; i--)
 {
 if (max_from_right < arr[i])
 { 
 max_from_right = arr[i];
 cout << max_from_right << " ";
 }
 } 
}
/* Driver program to test above function*/
int main()
{
 int arr[] = {16, 17, 4, 3, 5, 2};
 int n = sizeof(arr)/sizeof(arr[0]);
 printLeaders(arr, n);
 return 0;
}

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 \$Θ(nlogn)\$
  4. Solves it in time \$Θ(n^2)\$

At the start the right most element will always be a leader. If an element is greater than our current_max, it will a leader. Add this element to leaders. Set current_max to this element and carry on leftward. Time complexity would be \$O(n)\$.

Can you give an algorithm for another way or better way to find leader in an array?

#include <iostream>
using namespace std;
/* C++ Function to print leaders in an array */
void printLeaders(int arr[], int size)
{
 int max_from_right = arr[size-1];
 /* Rightmost element is always leader */
 cout << max_from_right << " ";
 for (int i = size-2; i >= 0; i--)
 {
 if (max_from_right < arr[i])
 { 
 max_from_right = arr[i];
 cout << max_from_right << " ";
 }
 } 
}
/* Driver program to test above function*/
int main()
{
 int arr[] = {16, 17, 4, 3, 5, 2};
 int n = sizeof(arr)/sizeof(arr[0]);
 printLeaders(arr, n);
 return 0;
}

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 \$Θ(n \log n)\$
  4. Solves it in time \$Θ(n^2)\$

At the start the right most element will always be a leader. If an element is greater than our current_max, it will a leader. Add this element to leaders. Set current_max to this element and carry on leftward. Time complexity would be \$O(n)\$.

Can you give an algorithm for another way or better way to find leader in an array?

#include <iostream>
using namespace std;
/* C++ Function to print leaders in an array */
void printLeaders(int arr[], int size)
{
 int max_from_right = arr[size-1];
 /* Rightmost element is always leader */
 cout << max_from_right << " ";
 for (int i = size-2; i >= 0; i--)
 {
 if (max_from_right < arr[i])
 { 
 max_from_right = arr[i];
 cout << max_from_right << " ";
 }
 } 
}
/* Driver program to test above function*/
int main()
{
 int arr[] = {16, 17, 4, 3, 5, 2};
 int n = sizeof(arr)/sizeof(arr[0]);
 printLeaders(arr, n);
 return 0;
}
added 4 characters in body; edited tags; edited title
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

Algorithm in another way to find leaderleaders in an array

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 \$Θ(nlogn)\$
  4. Solves it in time \$Θ(n^2)\$

At the start the right most element will always be a leader. If an element is greater than our current_max, it will a leader. Add this element to leaders. Set current_max to this element and carry on leftward. Time complexity would be \$O(n)\$.

Can you give an algorithm infor another way or better way to find leader in an array?

#include <iostream>
using namespace std;
/* C++ Function to print leaders in an array */
void printLeaders(int arr[], int size)
{
 int max_from_right = arr[size-1];
 /* Rightmost element is always leader */
 cout << max_from_right << " ";
 for (int i = size-2; i >= 0; i--)
 {
 if (max_from_right < arr[i])
 { 
 max_from_right = arr[i];
 cout << max_from_right << " ";
 }
 } 
}
/* Driver program to test above function*/
int main()
{
 int arr[] = {16, 17, 4, 3, 5, 2};
 int n = sizeof(arr)/sizeof(arr[0]);
 printLeaders(arr, n);
 return 0;
}

Algorithm in another way to find leader in array

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 \$Θ(nlogn)\$
  4. Solves it in time \$Θ(n^2)\$

At the start the right most element will always be a leader. If an element is greater than our current_max, it will a leader. Add this element to leaders. Set current_max to this element and carry on leftward. Time complexity would be \$O(n)\$.

Can you give algorithm in another way or better way to find leader in an array?

#include <iostream>
using namespace std;
/* C++ Function to print leaders in an array */
void printLeaders(int arr[], int size)
{
 int max_from_right = arr[size-1];
 /* Rightmost element is always leader */
 cout << max_from_right << " ";
 for (int i = size-2; i >= 0; i--)
 {
 if (max_from_right < arr[i])
 { 
 max_from_right = arr[i];
 cout << max_from_right << " ";
 }
 } 
}
/* Driver program to test above function*/
int main()
{
 int arr[] = {16, 17, 4, 3, 5, 2};
 int n = sizeof(arr)/sizeof(arr[0]);
 printLeaders(arr, n);
 return 0;
}

Algorithm to find leaders in an array

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 \$Θ(nlogn)\$
  4. Solves it in time \$Θ(n^2)\$

At the start the right most element will always be a leader. If an element is greater than our current_max, it will a leader. Add this element to leaders. Set current_max to this element and carry on leftward. Time complexity would be \$O(n)\$.

Can you give an algorithm for another way or better way to find leader in an array?

#include <iostream>
using namespace std;
/* C++ Function to print leaders in an array */
void printLeaders(int arr[], int size)
{
 int max_from_right = arr[size-1];
 /* Rightmost element is always leader */
 cout << max_from_right << " ";
 for (int i = size-2; i >= 0; i--)
 {
 if (max_from_right < arr[i])
 { 
 max_from_right = arr[i];
 cout << max_from_right << " ";
 }
 } 
}
/* Driver program to test above function*/
int main()
{
 int arr[] = {16, 17, 4, 3, 5, 2};
 int n = sizeof(arr)/sizeof(arr[0]);
 printLeaders(arr, n);
 return 0;
}
deleted 43 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Algorithm in another way to find leader in array?

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 \$Θ(nlogn)\$
  4. Solves it in time \$Θ(n^2)\$

AnAt the start the right most element in an array X is calledwill always be a leader if it. If an element is greater than all elements to the right ofour current_max, it in Xwill a leader. The bestAdd this element to leaders. Set current_max to this element and carry on leftward. Time complexity would be \$O(n)\$.

Can you give algorithm in another way or better way to find all leadersleader 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 Θ(nlogn)

  4. Solves it in time Θ(n^2)


My attempt :?

#include <iostream>
using namespace std;
/* C++ Function to print leaders in an array */
void printLeaders(int arr[], int size)
{
 int max_from_right = arr[size-1];
 /* Rightmost element is always leader */
 cout << max_from_right << " ";
 for (int i = size-2; i >= 0; i--)
 {
 if (max_from_right < arr[i])
 { 
 max_from_right = arr[i];
 cout << max_from_right << " ";
 }
 } 
}
/* Driver program to test above function*/
int main()
{
 int arr[] = {16, 17, 4, 3, 5, 2};
 int n = sizeof(arr)/sizeof(arr[0]);
 printLeaders(arr, n);
 return 0;
}

At the start the right most element will always be a leader. If an element is greater than our current_max, it will a leader. Add this element to leaders. Set current_max to this element and carry on leftward. Time Complexity would be O(n).

Can you give algorithm in another way or better way to find leader in array ?

Algorithm in another way to find leader in array?

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 Θ(nlogn)

  4. Solves it in time Θ(n^2)


My attempt :

#include <iostream>
using namespace std;
/* C++ Function to print leaders in an array */
void printLeaders(int arr[], int size)
{
 int max_from_right = arr[size-1];
 /* Rightmost element is always leader */
 cout << max_from_right << " ";
 for (int i = size-2; i >= 0; i--)
 {
 if (max_from_right < arr[i])
 { 
 max_from_right = arr[i];
 cout << max_from_right << " ";
 }
 } 
}
/* Driver program to test above function*/
int main()
{
 int arr[] = {16, 17, 4, 3, 5, 2};
 int n = sizeof(arr)/sizeof(arr[0]);
 printLeaders(arr, n);
 return 0;
}

At the start the right most element will always be a leader. If an element is greater than our current_max, it will a leader. Add this element to leaders. Set current_max to this element and carry on leftward. Time Complexity would be O(n).

Can you give algorithm in another way or better way to find leader in array ?

Algorithm in another way to find leader in array

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 \$Θ(nlogn)\$
  4. Solves it in time \$Θ(n^2)\$

At the start the right most element will always be a leader. If an element is greater than our current_max, it will a leader. Add this element to leaders. Set current_max to this element and carry on leftward. Time complexity would be \$O(n)\$.

Can you give algorithm in another way or better way to find leader in an array?

#include <iostream>
using namespace std;
/* C++ Function to print leaders in an array */
void printLeaders(int arr[], int size)
{
 int max_from_right = arr[size-1];
 /* Rightmost element is always leader */
 cout << max_from_right << " ";
 for (int i = size-2; i >= 0; i--)
 {
 if (max_from_right < arr[i])
 { 
 max_from_right = arr[i];
 cout << max_from_right << " ";
 }
 } 
}
/* Driver program to test above function*/
int main()
{
 int arr[] = {16, 17, 4, 3, 5, 2};
 int n = sizeof(arr)/sizeof(arr[0]);
 printLeaders(arr, n);
 return 0;
}
added 7 characters in body
Source Link
BCdotWEB
  • 11.4k
  • 2
  • 28
  • 45
Loading
Source Link
hululu
  • 131
  • 1
  • 5
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /