problem no 42:(array problem):return the count of the elements which are less than or equal to x.
QUESTION:
Given an sorted array A of size N. Find number of elements which are less than or equal to given element X.
Example 1:
Input:
N = 6
A[] = {1, 2, 4, 5, 8, 10}
X = 9
Output:
5
Example 2:
Input:
N = 7
A[] = {1, 2, 2, 2, 5, 7, 9}
X = 2
Output:
4
ANSWER:
int countOfElements(int arr[], int n, int x)
{
int count=0;
for(int i=0;i<n;i++)
{
if(arr[i]<=x)
{
count =count+1;
}
}
return count;
}
Comments
Post a Comment