Make max elements in B[] equal to that of A[] by adding/subtracting integers in range [0, K]
Last Updated :
13 Sep, 2021
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++
#include <bits/stdc++.h>
using namespace std;
void countElement( int A[], int N,
int B[], int M, int K)
{
int cnt = 0;
for ( int i = 0; i < M; i++) {
int currentElement = B[i];
for ( int j = 0; j < N; j++) {
int diff
= abs (currentElement - A[j]);
if (diff <= K) {
cnt++;
break ;
}
}
}
cout << cnt;
}
int main()
{
int A[] = { 100, 65, 35, 85, 55 };
int B[] = { 30, 60, 75, 95 };
int K = 5;
int N = sizeof (A) / sizeof (A[0]);
int M = sizeof (B) / sizeof (B[0]);
countElement(A, N, B, M, K);
return 0;
}
|
Java
class GFG{
static void countElement( int A[], int N,
int B[], int M, int K)
{
int cnt = 0 ;
for ( int i = 0 ; i < M; i++)
{
int currentElement = B[i];
for ( int j = 0 ; j < N; j++)
{
int diff = Math.abs(
currentElement - A[j]);
if (diff <= K)
{
cnt++;
break ;
}
}
}
System.out.print(cnt);
}
public static void main(String[] args)
{
int A[] = { 100 , 65 , 35 , 85 , 55 };
int B[] = { 30 , 60 , 75 , 95 };
int K = 5 ;
int N = A.length;
int M = B.length;
countElement(A, N, B, M, K);
}
}
|
Python3
def countElement(A, N, B, M, K):
cnt = 0
for i in range (M):
currentElement = B[i]
for j in range (N):
diff = abs (currentElement - A[j])
if (diff < = K):
cnt + = 1
break
print (cnt)
if __name__ = = '__main__' :
A = [ 100 , 65 , 35 , 85 , 55 ]
B = [ 30 , 60 , 75 , 95 ]
N = len (A)
M = len (B)
K = 5
countElement(A, N, B, M, K)
|
C#
using System;
class GFG{
static void countElement( int []A, int N,
int []B, int M, int K)
{
int cnt = 0;
for ( int i = 0; i < M; i++)
{
int currentElement = B[i];
for ( int j = 0; j < N; j++)
{
int diff = Math.Abs(
currentElement - A[j]);
if (diff <= K)
{
cnt++;
break ;
}
}
}
Console.Write(cnt);
}
public static void Main(String[] args)
{
int []A = { 100, 65, 35, 85, 55 };
int []B = { 30, 60, 75, 95 };
int K = 5;
int N = A.Length;
int M = B.Length;
countElement(A, N, B, M, K);
}
}
|
Javascript
<script>
function countElement(A, N, B, M, K)
{
let cnt = 0;
for (let i = 0; i < M; i++)
{
let currentElement = B[i];
for (let j = 0; j < N; j++)
{
let diff = Math.abs(
currentElement - A[j]);
if (diff <= K)
{
cnt++;
break ;
}
}
}
document.write(cnt);
}
let A = [ 100, 65, 35, 85, 55 ];
let B = [ 30, 60, 75, 95 ];
let K = 5;
let N = A.length;
let M = B.length;
countElement(A, N, B, M, K);
</script>
|
Time Complexity: O(N*M), where N and M are the lengths of the arrays A[] and B[].
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...