给定数组中可二进制搜索的元素的计数
给定一个由N个整数组成的数组arr[] ,任务是找到给定数组中可二进制搜索的整数的最大计数。
例子:
Input: arr[] = {1, 3, 2}
Output: 2
Explanation: arr[0], arr[1] can be found.
Input: arr[] = {3, 2, 1, 10, 23, 22, 21}
Output: 3
Explanation: arr[1], arr[3], arr[5] can be found using binary search irrespective of whether the array is sorted or not.
方法:给定的问题可以通过使用二进制搜索方法在数组中分别搜索每个元素并增加数组中存在的整数的计数来解决。请按照以下步骤解决问题:
- 创建一个变量count = 0 ,它将存储可二进制搜索的元素的计数。
- 对于每个元素,在[0, N)范围内执行二进制搜索,如下所示:
- 将变量l初始化为0并将r初始化为N-1并对arr[i]执行二进制搜索。
- 对于 while 循环的每次迭代,直到l小于等于r,计算由(l + r)/2表示的中间值。
- 如果arr[mid]等于arr[i]则将count增加1 。
- 如果arr[mid]小于arr[i],则将l更改为mid + 1 。
- 否则,将r更改为mid – 1 。
- 最终答案将存储在变量count中。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the total count of
// elements that are binary searchable
int totalBinarySearchable(vector arr)
{
// Stores the count of element that
// are binary searchable
int count = 0;
int N = arr.size();
// For each element check if it can
// be found by doing a binary search
for (int i = 0; i < N; i++) {
// Binary search range
int l = 0, r = N - 1;
// Do a binary Search
while (l <= r) {
int mid = (l + r) / 2;
// Array element found
if (arr[mid] == arr[i]) {
count++;
break;
}
if (arr[mid] < arr[i]) {
l = mid + 1;
}
else {
r = mid - 1;
}
}
}
// Return the total count
return count;
}
// Driver Code
int main()
{
vector arr = { 3, 2, 1, 10,
23, 22, 21 };
cout << totalBinarySearchable(arr);
return 0;
}
Java
// Java code for the above approach
import java.io.*;
class GFG
{
// Function to find the total count of
// elements that are binary searchable
static int totalBinarySearchable(int[] arr)
{
// Stores the count of element that
// are binary searchable
int count = 0;
int N = arr.length;
// For each element check if it can
// be found by doing a binary search
for (int i = 0; i < N; i++) {
// Binary search range
int l = 0, r = N - 1;
// Do a binary Search
while (l <= r) {
int mid = (l + r) / 2;
// Array element found
if (arr[mid] == arr[i]) {
count++;
break;
}
if (arr[mid] < arr[i]) {
l = mid + 1;
}
else {
r = mid - 1;
}
}
}
// Return the total count
return count;
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 3, 2, 1, 10, 23, 22, 21 };
System.out.println(totalBinarySearchable(arr));
}
}
// This code is contributed by Potta Lokesh
Python3
# python program for the above approach
# Function to find the total count of
# elements that are binary searchable
def totalBinarySearchable(arr):
# Stores the count of element that
# are binary searchable
count = 0
N = len(arr)
# For each element check if it can
# be found by doing a binary search
for i in range(0, N):
# Binary search range
l = 0
r = N - 1
# Do a binary Search
while (l <= r):
mid = (l + r) // 2
# Array element found
if (arr[mid] == arr[i]):
count += 1
break
if (arr[mid] < arr[i]):
l = mid + 1
else:
r = mid - 1
# Return the total count
return count
# Driver Code
if __name__ == "__main__":
arr = [3, 2, 1, 10,
23, 22, 21]
print(totalBinarySearchable(arr))
# This code is contributed by rakeshsahni
C#
// C# code for the above approach
using System;
public class GFG
{
// Function to find the total count of
// elements that are binary searchable
static int totalBinarySearchable(int[] arr)
{
// Stores the count of element that
// are binary searchable
int count = 0;
int N = arr.Length;
// For each element check if it can
// be found by doing a binary search
for (int i = 0; i < N; i++) {
// Binary search range
int l = 0, r = N - 1;
// Do a binary Search
while (l <= r) {
int mid = (l + r) / 2;
// Array element found
if (arr[mid] == arr[i]) {
count++;
break;
}
if (arr[mid] < arr[i]) {
l = mid + 1;
}
else {
r = mid - 1;
}
}
}
// Return the total count
return count;
}
// Driver Code
public static void Main(string[] args)
{
int[] arr = { 3, 2, 1, 10, 23, 22, 21 };
Console.WriteLine(totalBinarySearchable(arr));
}
}
// This code is contributed by rrrtnx.
Javascript
输出:
3
时间复杂度: O(N*log(N))
辅助空间: O(1)