# Make max elements in B[] equal to that of A[] by adding/subtracting integers in range [0, K]

Given two arrays A[] and B[] and an integer K, the task is to maximize the count of integers of array B[] that can be made equal with array A[] by adding or subtracting any integer in the range [0, K].

Examples:

Input: K=5, A[] = [100, 65, 35, 85, 55], B[] = [30, 60, 75, 95]
Output: 3
Explanation:
30 + 5, 60 + 5, 95 + 5 gives the values which are equal with few elements of array A[].

Input: K = 5, A[] = [10, 20, 30, 40, 50], B[] = [1, 20, 3]
Output: 1
Explanation:
Only the 2nd value can be made equal, Since its value [20] can be changed to [20] by  adding/subtracting 0 from it.

Approach: The idea is to check whether the absolute difference between elements of the array B[] with any element of the array A[] is less than or equals to K. If yes then include this in the count. Print the count all such elements after checking the above condition for all the elements in the array B[].

Below is the implementation of the above approach:

## 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`

Output:

```3

```

Time Complexity: O(N*M), where N and M are the lengths of the arrays A[] and B[].
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.