Answer to Question #230992 in C++ for HADii

Question #230992

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.


1
Expert's answer
2021-08-30T01:36:07-0400
#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;
}






Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS