Write a program to print all the LEADERS in the array. An element is leader if it is greater than all the elements to its right side. And the rightmost element is always a leader. For example in the array {16, 17, 4, 3, 5, 2}, leaders are 17, 5 and 2.
#include <iostream>
using namespace std;
int main()
{
int n;
int* numbers;
cout << "Enter the number of elements in array: ";
cin>> n;
numbers=new int[n];
for(int i=0;i<n;i++){
cout<<"Ente element "<<(i+1)<<": ";
cin>>numbers[i];
}
cout << "All the LEADERS in the array: "<< endl;
for(int i=0;i<n-1;i++){
bool isLeader=true;
for(int j=i+1;j<n;j++){
if(numbers[i]<numbers[j]){
isLeader=false;
}
}
if(isLeader){
cout<<numbers[i]<<" ";
}
}
cout<<numbers[n-1]<<" "<<endl<<endl;
delete[] numbers;
system("pause");
return 0;
}
Comments
Leave a comment