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
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function countOfElements() which takes the array A[], its size N and an integer X as inputs and returns the number of elements which are less than or equal to given element.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 10^{5}
1 <= A_{i }<= 10^{5}
0 <= X <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



def countOfElements( a, n, x):

    





Solutions:
def countOfElements(a, n, x):
	low = 0
	high = n - 1
	position = -1
	while low <= high:
		mid = low + (high - low) // 2
		if a[mid] <= x:
			position = mid
			low = mid + 1
		else:
			high = mid - 1
	return position + 1


def countOfElements(a, n, x):
	count = 0
	for i in range(n):
		if a[i] <= x:
			count += 1
		else:
			break
	return count


def countOfElements(a, n, x):
	count = 0
	for i in a:
		if i <= x:
			count += 1
	return count


def countOfElements(a, n, x):
	cnt = 0
	i = 0
	while i < n:
		if a[i] <= x:
			cnt += 1
		i += 1
	return cnt


def countOfElements(a, n, x):
	c = 0
	for i in range(n):
		if x >= a[i]:
			c += 1
	return c


def countOfElements(a, n, x):
	try:
		count = 0
		for i in a:
			if i > x:
				return count
			count += 1
		return count
	except TypeError:
		return 0


def countOfElements(a, n, x):
	l = []
	for i in a:
		if i <= x:
			l.append(i)
	return len(l)


def countOfElements(arr, n, x):
	ele_count = 0
	for num in arr:
		if num <= x:
			ele_count += 1
		else:
			break
	return ele_count


def countOfElements(arr, n, x):
	count = 0
	for num in arr:
		if num <= x:
			count = count + 1
	return count


def countOfElements(a, n, x):
	if x >= max(a):
		return len(a)
	elif x < min(a):
		return 0
	elif x <= min(a):
		return a.count(x)
	else:
		for i in range(len(a) - 1, 0, -1):
			if x >= a[i]:
				return i + 1


def countOfElements(a, n, x):
	l = 0
	for i in range(n):
		if a[i] <= x:
			l += 1
		else:
			continue
	return l


def countOfElements(a, n, x):
	b = []
	for i in a:
		if i <= x:
			b.append(i)
		else:
			continue
	return len(b)


def countOfElements(A, n, X):
	(low, high) = (0, n - 1)
	while low <= high:
		mid = low + (high - low) // 2
		if A[mid] > X:
			high = mid - 1
		else:
			low = mid + 1
	return high + 1


def countOfElements(a, n, x):
	i = 0
	while i < n:
		if a[i] > x:
			break
		i += 1
	return i


def countOfElements(a, n, x):
	lst = list(a)
	count = 0
	for i in range(len(lst)):
		if lst[i] <= x:
			count += 1
	return count


def countOfElements(a, n, x):
	for i in range(n):
		if a[i] > x:
			return i
	return n


def countOfElements(a, n, x):
	list1 = []
	for i in range(n):
		if a[i] <= x:
			list1.append(a[i])
	return len(list1)


def countOfElements(a, n, x):
	return len(list(filter(lambda i: i <= x, a)))


def countOfElements(a, n, x):
	sum = 0
	for i in a:
		if i <= x:
			sum += 1
		else:
			sum += 0
	return sum