problem no 55: (problem on array):Leaders in an array.
QUESTION:
Given an array A of positive integers. Your task is to find the leaders in the array. An element of array is leader if it is greater than or equal to all the elements to its right side. The rightmost element is always a leader.
Example 1:
Input:
n = 6
A[] = {16,17,4,3,5,2}
Output: 17 5 2
Explanation: The first leader is 17
as it is greater than all the elements
to its right. Similarly, the next
leader is 5. The right most element
is always a leader so it is also
included.
Example 2:
Input:
n = 5
A[] = {1,2,3,4,0}
Output: 4 0
ANSWER:
vector<int> leaders(int a[], int n){
vector<int> vec;
int max=a[n-1];
vec.insert(vec.begin(), max);
for(int i=n-2;i>=0;i--)
{
if(a[i]>=max)
{
max=a[i];
vec.insert(vec.begin(), max);
}
}
return vec;
}
Comments
Post a Comment