problem no 55: (problem on array):Leaders in an array.

QUESTION:

Leaders in an array
Easy Accuracy: 49.96% Submissions: 41276 Points: 2

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

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.