Skip to content

Commit

Permalink
Merge pull request #3493 from Gourav1000/Leaders-in-an-array
Browse files Browse the repository at this point in the history
Create LeadersInAnArray
  • Loading branch information
ZoranPandovski authored Nov 1, 2022
2 parents f07e6c2 + 5ce823e commit 41c7b7a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions data_structures/Arrays/CPP/LeadersInAnArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include<iostream>
using namespace std;

/*C++ Function to print leaders in an array */
void printLeaders(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
int j;
for (j = i+1; j < size; j++)
{
if (arr[i] <=arr[j])
break;
}
if (j == size) // the loop didn't break
cout << arr[i] << " ";
}
}

/* 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;
}

0 comments on commit 41c7b7a

Please sign in to comment.