数组 B[] 中存在于 [A[i] + K, A[i] – K] 范围内的最大元素数
给定两个大小为N的数组A[]和大小为M的B[]和一个整数K ,任务是为每个元素A[i]从数组B[]中选择最多一个元素,使得该元素位于范围内[A[i] – K, A[i] + K] (对于0 <= i <= N – 1 ) 。打印可以从数组B[] 中选择的最大元素数。
例子:
Input: N = 4, A[] = {60, 45, 80, 60}, M = 3, B[] = {30, 60, 75}, K= 5
Output: 2
Explanation :
B[0] (= 30): Not present in any of the ranges [A[i] + K, A[i] – K].
B[1] (= 60): B[1] lies in the range [A[0] – K, A[0] + K], i.e. [55, 65].
B[2] (= 75): B[2] lies in the range [A[2] – K, A[2] + K], i.e. [75, 85].
Input: N = 3 A[] = {10, 20, 30}, M = 3, B[] = {5, 10, 15}, K = 10
Output: 2
朴素方法:解决问题的最简单方法是遍历数组A[] ,在数组B[]中线性搜索,如果选择了数组B[]的值,则标记为已访问。最后,打印数组B[]中可以选择的最大元素数。
时间复杂度: O(N * M)
辅助空间: O(M)
有效方法:对数组A[]和B[]进行排序,并尝试分配B[]的元素在[A[i] – K, A[i] + K] 范围内。请按照以下步骤解决问题:
- 对数组A[]和B[] 进行排序。
- 初始化一个变量,比如j为0,以在数组B[]中跟踪并计数为0以存储答案。
- 在[0, N – 1]范围内迭代并执行以下步骤:
- 在 while 循环中迭代直到j < M且B[j]< A[i] – K,然后将j的值增加1。
- 如果j的值小于M并且B[j]大于等于A[i] – K并且B[j]小于等于A[i] + K则将count和j的值增加1.
- 完成上述步骤后,打印count的值作为答案的最终值。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the maximum number of
// elements that can be selected from array
// B[] lying in the range [A[i] - K, A[i] + K]
int selectMaximumEle(int n, int m, int k,
int A[], int B[])
{
// Sort both arrays
sort(A, A + n);
sort(B, B + m);
int j = 0, count = 0;
// Iterate in the range[0, N-1]
for (int i = 0; i < n; i++) {
// Increase the value of j till
// B[j] is smaller than A[i]
while (j < m && B[j] < A[i] - k) {
j++;
}
// Increasing count variable when B[j]
// lies in the range [A[i]-K, A[i]+K]
if (j < m && B[j] >= A[i] - k
&& B[j] <= A[i] + k) {
count++;
j++;
}
}
// Finally, return the answer
return count;
}
// Driver Code
int main()
{
// Given Input
int N = 3, M = 3, K = 10;
int A[] = { 10, 20, 30 };
int B[] = { 5, 10, 15 };
// Function Call
cout << selectMaximumEle(N, M, K, A, B) << endl;
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.Arrays;
class GFG
{
// Function to count the maximum number of
// elements that can be selected from array
// B[] lying in the range [A[i] - K, A[i] + K]
static int selectMaximumEle(int n, int m, int k,
int A[], int B[])
{
// Sort both arrays
Arrays.sort(A);
Arrays.sort(B);
int j = 0, count = 0;
// Iterate in the range[0, N-1]
for (int i = 0; i < n; i++) {
// Increase the value of j till
// B[j] is smaller than A[i]
while (j < m && B[j] < A[i] - k) {
j++;
}
// Increasing count variable when B[j]
// lies in the range [A[i]-K, A[i]+K]
if (j < m && B[j] >= A[i] - k
&& B[j] <= A[i] + k) {
count++;
j++;
}
}
// Finally, return the answer
return count;
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int N = 3, M = 3, K = 10;
int A[] = { 10, 20, 30 };
int B[] = { 5, 10, 15 };
// Function Call
System.out.println(selectMaximumEle(N, M, K, A, B));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python3 program for the above approach
# Function to count the maximum number of
# elements that can be selected from array
# B[] lying in the range [A[i] - K, A[i] + K]
def selectMaximumEle(n, m, k, A, B):
# Sort both arrays
A.sort()
B.sort()
j = 0
count = 0
# Iterate in the range[0, N-1]
for i in range(n):
# Increase the value of j till
# B[j] is smaller than A[i]
while (j < m and B[j] < A[i] - k):
j += 1
# Increasing count variable when B[j]
# lies in the range [A[i]-K, A[i]+K]
if (j < m and B[j] >= A[i] - k
and B[j] <= A[i] + k):
count += 1
j += 1
# Finally, return the answer
return count
# Driver Code
# Given Input
N = 3
M = 3
K = 10
A = [ 10, 20, 30 ]
B = [ 5, 10, 15 ]
# Function Call
print(selectMaximumEle(N, M, K, A, B))
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
class GFG{
// Function to count the maximum number of
// elements that can be selected from array
// B[] lying in the range [A[i] - K, A[i] + K]
static int selectMaximumEle(int n, int m, int k,
int[] A, int[] B)
{
// Sort both arrays
Array.Sort(A);
Array.Sort(B);
int j = 0, count = 0;
// Iterate in the range[0, N-1]
for(int i = 0; i < n; i++)
{
// Increase the value of j till
// B[j] is smaller than A[i]
while (j < m && B[j] < A[i] - k)
{
j++;
}
// Increasing count variable when B[j]
// lies in the range [A[i]-K, A[i]+K]
if (j < m && B[j] >= A[i] - k &&
B[j] <= A[i] + k)
{
count++;
j++;
}
}
// Finally, return the answer
return count;
}
// Driver code
public static void Main()
{
// Given Input
int N = 3, M = 3, K = 10;
int[] A = { 10, 20, 30 };
int[] B = { 5, 10, 15 };
// Function Call
Console.WriteLine(selectMaximumEle(N, M, K, A, B));
}
}
// This code is contributed by avijitmondal1998
Javascript
2
时间复杂度: O(N*log(N))
辅助空间: O(N)