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

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.