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

📅  最后修改于: 2021-09-06 11:30:07             🧑  作者: Mango

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


Javascript


输出:
3

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live