Haga que los elementos máximos en B[] sean iguales a los de A[] sumando/restando números enteros en el rango [0, K]

Dados dos arreglos A[] y B[] y un entero K , la tarea es maximizar el conteo de enteros del arreglo B[] que pueden igualarse con el arreglo A[] sumando o restando cualquier entero en el rango [0 , K] .

Ejemplos:

Entrada: K=5, A[] = [100, 65, 35, 85, 55], B[] = [30, 60, 75, 95]
Salida: 3
Explicación:
30 + 5, 60 + 5, 95 + 5 da los valores que son iguales con pocos elementos de la array A[].

Entrada: K = 5, A[] = [10, 20, 30, 40, 50], B[] = [1, 20, 3]
Salida: 1
Explicación:
Solo se puede igualar el 2º valor, ya que su valor [20] se puede cambiar a [20] sumando o restando 0.

Enfoque: La idea es verificar si la diferencia absoluta entre los elementos del arreglo B[] con cualquier elemento del arreglo A[] es menor o igual a K . En caso afirmativo, inclúyalo en el recuento. Imprima el recuento de todos esos elementos después de verificar la condición anterior para todos los elementos en la array B[] .

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ program for the above approach
#include <bits/stdc++.h>
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

<script>
// Javascript Script 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[]
function countElement(A, N, B, M, K)
{
   
    // To store the count of element
    let cnt = 0;
   
    // Traverse the array B[]
    for(let i = 0; i < M; i++)
    {
        let currentElement = B[i];
   
        // Traverse the array A[]
        for(let j = 0; j < N; j++)
        {
               
            // Find the difference
            let diff = Math.abs(
                       currentElement - A[j]);
   
            // If difference is atmost
            // K then increment the cnt
            if (diff <= K)
            {
                cnt++;
                break;
            }
        }
    }
   
    // Print the count
    document.write(cnt);
}
 
 
// Driver Code
 
    // Given array A[] and B[]
    let A = [ 100, 65, 35, 85, 55 ];
    let B = [ 30, 60, 75, 95 ];
   
    // Given K
    let K = 5;
   
    let N = A.length;
    let M = B.length;
   
    // Function call
    countElement(A, N, B, M, K);
      
</script>
Producción: 

3

 

Complejidad temporal: O(N*M), donde N y M son las longitudes de las arrays A[] y B[].
Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por jatindscl015 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *