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].
Input: K=5, A = [100, 65, 35, 85, 55], B = [30, 60, 75, 95]
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]
Only the 2nd value can be made equal, Since its value  can be changed to  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:
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.
- Generate a pair of integers from a range [L, R] whose LCM also lies within the range
- Count of Array elements to be divided by 2 to make at least K elements equal
- Count of elements to be multiplied with integers to make each pair of Array a perfect square
- Minimum removals from array to make max - min <= K
- Noble integers in an array (count of greater elements is equal to value)
- Minimum product of k integers in an array of positive Integers
- Mode in a stream of integers (running integers)
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Make all array elements equal with minimum cost
- Minimum operation to make all elements equal in array
- Minimum number of increment-other operations to make all array elements equal.
- Minimum increment by k operations to make all elements equal
- Make array elements equal in Minimum Steps
- Find the number of operations required to make all array elements Equal
- Minimum Bitwise AND operations to make any two array elements equal
- Minimum Increment / decrement to make array elements equal
- Minimum number of moves to make all elements equal
- Make all elements of an array equal with the given operation
- Minimum operations required to make all the array elements equal
- Find the minimum number of operations required to make all array elements equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.