📌  相关文章
📜  通过加/减范围[0,K]的整数,使B []中的最大元素等于A []的元素

📅  最后修改于: 2021-05-19 19:24:41             🧑  作者: Mango

给定两个数组A []B []以及一个整数K ,任务是通过添加或减去范围[0 ]中的任何整数来最大化可以与数组A []相等的数组B []的整数数量,K]

例子:

方法:想法是检查数组B []的元素与数组A []的任何元素之间的绝对差是否小于或等于K。如果是,则将其包括在计数中。在检查完数组B []中所有元素的上述条件后,打印所有这些元素的计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function that count the number of
// integers from array B[] such that
// subtracting element in the range
// [0, K] given any element in A[]
void countElement(int A[], int N,
                  int B[], int M, int K)
{
  
    // To store the count of element
    int cnt = 0;
  
    // Traverse the array B[]
    for (int i = 0; i < M; i++) {
  
        int currentElement = B[i];
  
        // Traverse the array A[]
        for (int j = 0; j < N; j++) {
  
            // Find the difference
            int diff
                = abs(currentElement - A[j]);
  
            // If difference is atmost
            // K then increment the cnt
            if (diff <= K) {
                cnt++;
                break;
            }
        }
    }
  
    // Print the count
    cout << cnt;
}
  
// Driver Code
int main()
{
    // Given array A[] and B[]
    int A[] = { 100, 65, 35, 85, 55 };
    int B[] = { 30, 60, 75, 95 };
  
    // Given K
    int K = 5;
  
    int N = sizeof(A) / sizeof(A[0]);
    int M = sizeof(B) / sizeof(B[0]);
  
    // Function Call
    countElement(A, N, B, M, K);
    return 0;
}


Java
// Java program for the above approach
class GFG{
  
// Function that count the number of
// integers from array B[] such that
// subtracting element in the range
// [0, K] given any element in A[]
static void countElement(int A[], int N,
                         int B[], int M, int K)
{
  
    // To store the count of element
    int cnt = 0;
  
    // Traverse the array B[]
    for(int i = 0; i < M; i++) 
    {
        int currentElement = B[i];
  
        // Traverse the array A[]
        for(int j = 0; j < N; j++)
        {
              
            // Find the difference
            int diff = Math.abs(
                       currentElement - A[j]);
  
            // If difference is atmost
            // K then increment the cnt
            if (diff <= K)
            {
                cnt++;
                break;
            }
        }
    }
  
    // Print the count
    System.out.print(cnt);
}
  
// Driver Code
public static void main(String[] args)
{
      
    // Given array A[] and B[]
    int A[] = { 100, 65, 35, 85, 55 };
    int B[] = { 30, 60, 75, 95 };
  
    // Given K
    int K = 5;
  
    int N = A.length;
    int M = B.length;
  
    // Function call
    countElement(A, N, B, M, K);
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 program to implement
# the above approach
  
# Function that count the number of
# integers from array B such that
# subtracting element in the range
# [0, K] given any element in A
def countElement(A, N, B, M, K):
  
    # To store the count of element
    cnt = 0
  
    # Traverse the array B
    for i in range(M):
        currentElement = B[i]
  
        # Traverse the array A
        for j in range(N):
  
            # Find the difference
            diff = abs(currentElement - A[j])
  
            # If difference is atmost
            # K then increment the cnt 
            if(diff <= K):
                cnt += 1
                break
  
    # Print the count
    print(cnt)
  
# Driver Code
if __name__ == '__main__':
  
    # Given array A and B
    A = [ 100, 65, 35, 85, 55 ]
    B = [ 30, 60, 75, 95 ]
  
    N = len(A)
    M = len(B)
  
    # Given K
    K = 5
  
    # Function call
    countElement(A, N, B, M, K)
  
# This code is contributed by Shivam Singh


C#
// C# program for the above approach
using System;
class GFG{
  
// Function that count the number of
// integers from array []B such that
// subtracting element in the range
// [0, K] given any element in []A
static void countElement(int []A, int N,
                         int []B, int M, int K)
{
  
    // To store the count of element
    int cnt = 0;
  
    // Traverse the array []B
    for(int i = 0; i < M; i++) 
    {
        int currentElement = B[i];
  
        // Traverse the array []A
        for(int j = 0; j < N; j++)
        {
              
            // Find the difference
            int diff = Math.Abs(
                       currentElement - A[j]);
  
            // If difference is atmost
            // K then increment the cnt
            if (diff <= K)
            {
                cnt++;
                break;
            }
        }
    }
  
    // Print the count
    Console.Write(cnt);
}
  
// Driver Code
public static void Main(String[] args)
{
      
    // Given array []A and []B
    int []A = { 100, 65, 35, 85, 55 };
    int []B = { 30, 60, 75, 95 };
  
    // Given K
    int K = 5;
  
    int N = A.Length;
    int M = B.Length;
  
    // Function call
    countElement(A, N, B, M, K);
}
}
  
// This code is contributed by Rohit_ranjan


输出:
3

时间复杂度: O(N * M),其中N和M是数组A []和B []的长度。
辅助空间: O(1)