排序数组中较小或相等元素的计数
给定一个大小为 n 的排序数组。查找小于或等于给定元素的多个元素。
例子:
Input : arr[] = {1, 2, 4, 5, 8, 10}
key = 9
Output : 5
Elements less than or equal to 9 are 1, 2,
4, 5, 8 therefore result will be 5.
Input : arr[] = {1, 2, 2, 2, 5, 7, 9}
key = 2
Output : 4
Elements less than or equal to 2 are 1, 2,
2, 2 therefore result will be 4.
朴素的方法:线性搜索整个数组并计算小于或等于键的元素。
有效的方法:由于整个数组是排序的,我们可以使用二进制搜索来查找结果。
情况1:当key存在于数组中时,key的最后一个位置就是结果。
情况 2:当 key 不存在于数组中时,如果 key 大于 mid,我们将忽略左半部分。如果 key 小于 mid,我们忽略右半部分。我们总是会遇到 key 出现在中间元素之前的情况。
C++
// C++ program to count smaller or equal
// elements in sorted array.
#include
using namespace std;
// A binary search function. It returns
// number of elements less than of equal
// to given key
int binarySearchCount(int arr[], int n, int key)
{
int left = 0, right = n;
int mid;
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 program to test binarySearchCount()
int main()
{
int arr[] = { 1, 2, 4, 5, 8, 10 };
int key = 11;
int n = sizeof(arr) / sizeof(arr[0]);
cout << binarySearchCount(arr, n, key);
return 0;
}
Java
// 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.
Python3
# Python program to
# count smaller or equal
# elements in sorted array.
# A binary search function.
# It returns
# number of elements
# less than of equal
# to given key
def binarySearchCount(arr, n, key):
left = 0
right = n
mid = 0
while (left < right):
mid = (right + left)//2
# 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 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 and arr[mid] > key):
mid-= 1
# Return mid + 1 because
# of 0-based indexing
# of array
return mid + 1
# Driver code
arr = [1, 2, 4, 5, 8, 10]
key = 11
n = len(arr)
print(binarySearchCount(arr, n, key))
# This code is contributed
# by Anant Agarwal.
C#
// C# program to count smaller or
// equal elements in sorted array.
using System;
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;
int right = n;
int mid = 0;
while (left < right) {
mid = (right + left) / 2;
// 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
static public void Main()
{
int[] arr = { 1, 2, 4, 5, 8, 10 };
int key = 11;
int n = arr.Length;
Console.Write(binarySearchCount(arr, n, key));
}
}
// This code is contributed by ajit.
PHP
-1 && $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 ($arr[$mid] > $key)
$mid--;
// Return mid + 1 because
// of 0-based indexing
// of array
return $mid + 1;
}
// Driver Code
$arr = array (1, 2, 4,
5, 8, 10);
$key = 11;
$n = sizeof($arr) ;
echo binarySearchCount($arr, $n, $key);
// This code is contributed by ajit
?>
Javascript
C++
// C++ program to count smaller or equal
// elements in sorted array
#include
using namespace std;
// A binary search function to return
// the number of elements less than
// or equal to the given key
int binarySearchCount(int arr[], int n, int key)
{
int left = 0;
int right = n - 1;
int count = 0;
while (left <= right) {
int mid = (right + left) / 2;
// Check if middle element is
// less than or equal to key
if (arr[mid] <= key) {
// At least (mid + 1) elements are there
// whose values are less than
// or equal to key
count = mid + 1;
left = mid + 1;
}
// If key is smaller, ignore right half
else
right = mid - 1;
}
return count;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 4, 11, 11, 16 };
int key = 11;
int n = sizeof(arr) / sizeof(arr[0]);
cout << binarySearchCount(arr, n, key);
return 0;
}
Java
// Java program to count smaller or equal
import java.io.*;
class GFG
{
// A binary search function to return
// the number of elements less than
// or equal to the given key
static int binarySearchCount(int arr[],
int n, int key)
{
int left = 0;
int right = n - 1;
int count = 0;
while (left <= right)
{
int mid = (right + left) / 2;
// Check if middle element is
// less than or equal to key
if (arr[mid] <= key)
{
// At least (mid + 1) elements are there
// whose values are less than
// or equal to key
count = mid + 1;
left = mid + 1;
}
// If key is smaller, ignore right half
else
right = mid - 1;
}
return count;
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 1, 2, 4, 11, 11, 16 };
int key = 11;
int n = arr.length;
System.out.println (binarySearchCount(arr, n, key));
}
}
// The code is contributed by Sachin.
Python3
# Python3 program to count smaller or equal
# elements in sorted array
# A binary search function to return
# the number of elements less than
# or equal to the given key
def binarySearchCount(arr, n, key):
left = 0
right = n - 1
count = 0
while (left <= right):
mid = int((right + left) / 2)
# Check if middle element is
# less than or equal to key
if (arr[mid] <= key):
# At least (mid + 1) elements are there
# whose values are less than
# or equal to key
count = mid + 1
left = mid + 1
# If key is smaller, ignore right half
else:
right = mid - 1
return count
# Driver code
arr = [ 1, 2, 4, 11, 11, 16 ]
key = 11
n = len(arr)
print( binarySearchCount(arr, n, key))
# This code is contributed by Arnab Kundu
C#
// C# program to count smaller or equal
using System;
class GFG
{
// A binary search function to return
// the number of elements less than
// or equal to the given key
static int binarySearchCount(int []arr,
int n, int key)
{
int left = 0;
int right = n - 1;
int count = 0;
while (left <= right)
{
int mid = (right + left) / 2;
// Check if middle element is
// less than or equal to key
if (arr[mid] <= key)
{
// At least (mid + 1) elements are there
// whose values are less than
// or equal to key
count = mid + 1;
left = mid + 1;
}
// If key is smaller,
// ignore right half
else
right = mid - 1;
}
return count;
}
// Driver code
public static void Main (String[] args)
{
int []arr = { 1, 2, 4, 11, 11, 16 };
int key = 11;
int n = arr.Length;
Console.WriteLine(binarySearchCount(arr, n, key));
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出
6
虽然这个解决方案平均表现更好,但这个解决方案的最坏情况时间复杂度仍然是 O(n)。
上述程序可以使用更简化的二分搜索来实现。这个想法是检查中间元素是否大于给定元素,然后将右索引更新为mid – 1但如果中间元素小于或等于键,则更新答案为mid + 1 ,左索引为mid + 1 。
下面是上述方法的实现:
C++
// C++ program to count smaller or equal
// elements in sorted array
#include
using namespace std;
// A binary search function to return
// the number of elements less than
// or equal to the given key
int binarySearchCount(int arr[], int n, int key)
{
int left = 0;
int right = n - 1;
int count = 0;
while (left <= right) {
int mid = (right + left) / 2;
// Check if middle element is
// less than or equal to key
if (arr[mid] <= key) {
// At least (mid + 1) elements are there
// whose values are less than
// or equal to key
count = mid + 1;
left = mid + 1;
}
// If key is smaller, ignore right half
else
right = mid - 1;
}
return count;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 4, 11, 11, 16 };
int key = 11;
int n = sizeof(arr) / sizeof(arr[0]);
cout << binarySearchCount(arr, n, key);
return 0;
}
Java
// Java program to count smaller or equal
import java.io.*;
class GFG
{
// A binary search function to return
// the number of elements less than
// or equal to the given key
static int binarySearchCount(int arr[],
int n, int key)
{
int left = 0;
int right = n - 1;
int count = 0;
while (left <= right)
{
int mid = (right + left) / 2;
// Check if middle element is
// less than or equal to key
if (arr[mid] <= key)
{
// At least (mid + 1) elements are there
// whose values are less than
// or equal to key
count = mid + 1;
left = mid + 1;
}
// If key is smaller, ignore right half
else
right = mid - 1;
}
return count;
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 1, 2, 4, 11, 11, 16 };
int key = 11;
int n = arr.length;
System.out.println (binarySearchCount(arr, n, key));
}
}
// The code is contributed by Sachin.
Python3
# Python3 program to count smaller or equal
# elements in sorted array
# A binary search function to return
# the number of elements less than
# or equal to the given key
def binarySearchCount(arr, n, key):
left = 0
right = n - 1
count = 0
while (left <= right):
mid = int((right + left) / 2)
# Check if middle element is
# less than or equal to key
if (arr[mid] <= key):
# At least (mid + 1) elements are there
# whose values are less than
# or equal to key
count = mid + 1
left = mid + 1
# If key is smaller, ignore right half
else:
right = mid - 1
return count
# Driver code
arr = [ 1, 2, 4, 11, 11, 16 ]
key = 11
n = len(arr)
print( binarySearchCount(arr, n, key))
# This code is contributed by Arnab Kundu
C#
// C# program to count smaller or equal
using System;
class GFG
{
// A binary search function to return
// the number of elements less than
// or equal to the given key
static int binarySearchCount(int []arr,
int n, int key)
{
int left = 0;
int right = n - 1;
int count = 0;
while (left <= right)
{
int mid = (right + left) / 2;
// Check if middle element is
// less than or equal to key
if (arr[mid] <= key)
{
// At least (mid + 1) elements are there
// whose values are less than
// or equal to key
count = mid + 1;
left = mid + 1;
}
// If key is smaller,
// ignore right half
else
right = mid - 1;
}
return count;
}
// Driver code
public static void Main (String[] args)
{
int []arr = { 1, 2, 4, 11, 11, 16 };
int key = 11;
int n = arr.Length;
Console.WriteLine(binarySearchCount(arr, n, key));
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出
5