- Given a sorted array A of size N. Find the number of elements that are less than or equal to given element X.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T
test cases follow. Each test case contains 3 lines of input. The first line contains an
integer N denoting the size of the array. Then the next line contains N space-separated
integers forming the array. The third line contains an element X.
Output:
For each test case, in a new line, print the number of elements which are less than or
equal to the given element.
Input:
2
6
1 2 4 5 8 10
9
7
1 2 2 2 5 7 9
2
Output:
5
4
// Java program to count smaller or equal
// elements in sorted array.
class GFG {
// A binary search function. It returns
// number of elements less than of equal
// to given key
static int binarySearchCount(int arr[], int n, int key)
{
int left = 0, right = n;
int mid = 0;
while (left < right) {
mid = (right + left) >> 1;
// Check if key is present in array
if (arr[mid] == key) {
// If duplicates are present it returns
// the position of last element
while (mid + 1 < n && arr[mid + 1] == key)
mid++;
break;
}
// If key is smaller, ignore right half
else if (arr[mid] > key)
right = mid;
// If key is greater, ignore left half
else
left = mid + 1;
}
// If key is not found in array then it will be
// before mid
while (mid > -1 && arr[mid] > key)
mid--;
// Return mid + 1 because of 0-based indexing
// of array
return mid + 1;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 4, 5, 8, 10 };
int key = 11;
int n = arr.length;
System.out.print(binarySearchCount(arr, n, key));
}
}
// This code is contributed by Anant Agarwal.
Given an array A of size N of integers. Your task is to find the minimum and
maximum elements in the array.
Input: The first line of input contains an integer T denoting the number of test cases.
T test cases follow. Each test case contains 2 lines of input. The first line of each test
case contains the size of array N. The following line contains elements of the array
separated by spaces.
Example: Input:
2
6
3 2 1 56 10000 167
5
1 345 234 21 56789
Output:
1 10000
1 56789
// Java program to count smaller or equal
// elements in sorted array.
class GFG {
// A binary search function. It returns
// number of elements less than of equal
// to given key
static int binarySearchCount(int arr[], int n, int key)
{
int left = 0, right = n;
int mid = 0;
while (left < right) {
mid = (right + left) >> 1;
// Check if key is present in array
if (arr[mid] == key) {
// If duplicates are present it returns
// the position of last element
while (mid + 1 < n && arr[mid + 1] == key)
mid++;
break;
}
// If key is smaller, ignore right half
else if (arr[mid] > key)
right = mid;
// If key is greater, ignore left half
else
left = mid + 1;
}
// If key is not found in array then it will be
// before mid
while (mid > -1 && arr[mid] > key)
mid--;
// Return mid + 1 because of 0-based indexing
// of array
return mid + 1;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 4, 5, 8, 10 };
int key = 11;
int n = arr.length;
System.out.print(binarySearchCount(arr, n, key));
}
}
// This code is contributed by Anant Agarwal.