Suma de elementos de un arreglo cuya diferencia con la media de otro arreglo es menor que k

Dadas dos arrays desordenadas arr1[] y arr2[] . Encuentra la suma de los elementos de arr1[] cuya diferencia con la media de arr2[] es < k .
Ejemplos: 
 

Entrada: arr1[] = {1, 2, 3, 4, 7, 9}, arr2[] = {0, 1, 2, 1, 1, 4}, k = 2 
Salida:
La media de la segunda array es 1,5 . 
Por lo tanto, 1, 2, 3 son los únicos elementos 
cuya diferencia con la media es menor que 2
Entrada: arr1[] = {5, 10, 2, 6, 1, 8, 6, 12}, arr2[] = {6, 5, 11, 4, 2, 3, 7}, k = 4 
Salida:
 

Enfoque: Calcule la media de la segunda array y luego recorra la primera array y calcule la suma de aquellos elementos cuya diferencia absoluta con la media es < k .
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <iostream>
using namespace std;
 
// Function for finding sum of elements
// whose diff with mean is not more than k
int findSumofEle(int arr1[], int m,
                 int arr2[], int n, int k)
{
    float arraySum = 0;
 
    // Find the mean of second array
    for (int i = 0; i < n; i++)
        arraySum += arr2[i];
    float mean = arraySum / n;
 
    // Find sum of elements from array1
    // whose difference with mean in not more than k
    int sumOfElements = 0;
    float difference;
 
    for (int i = 0; i < m; i++) {
        difference = arr1[i] - mean;
        if ((difference < 0) && (k > (-1) * difference)) {
            sumOfElements += arr1[i];
        }
        if ((difference >= 0) && (k > difference)) {
            sumOfElements += arr1[i];
        }
    }
 
    // Return result
    return sumOfElements;
}
 
// Driver code
int main()
{
    int arr1[] = { 1, 2, 3, 4, 7, 9 };
    int arr2[] = { 0, 1, 2, 1, 1, 4 };
    int k = 2;
    int m, n;
 
    m = sizeof(arr1) / sizeof(arr1[0]);
    n = sizeof(arr2) / sizeof(arr2[0]);
 
    cout << findSumofEle(arr1, m, arr2, n, k);
 
    return 0;
}

Java

// Java implementation of the approach
class GFG
{
     
// Function for finding sum of elements
// whose diff with mean is not more than k
static int findSumofEle(int []arr1, int m,
                int []arr2, int n, int k)
{
    float arraySum = 0;
 
    // Find the mean of second array
    for (int i = 0; i < n; i++)
        arraySum += arr2[i];
    float mean = arraySum / n;
 
    // Find sum of elements from array1
    // whose difference with mean in not more than k
    int sumOfElements = 0;
    float difference = 0;
 
    for (int i = 0; i < m; i++)
    {
        difference = arr1[i] - mean;
        if ((difference < 0) && (k > (-1) * difference))
        {
            sumOfElements += arr1[i];
        }
        if ((difference >= 0) && (k > difference))
        {
            sumOfElements += arr1[i];
        }
    }
 
    // Return result
    return sumOfElements;
}
 
// Driver code
public static void main (String[] args)
{
    int []arr1 = { 1, 2, 3, 4, 7, 9 };
    int []arr2 = { 0, 1, 2, 1, 1, 4 };
    int k = 2;
 
    int m = arr1.length;
    int n = arr2.length;
 
    System.out.println(findSumofEle(arr1, m, arr2, n, k));
}
}
 
// This code is contributed by mits

Python3

# Python3 implementation of the approach
 
# Function for finding sum of elements
# whose diff with mean is not more than k
def findSumofEle(arr1, m, arr2, n, k):
    arraySum = 0
 
    # Find the mean of second array
    for i in range(n):
        arraySum += arr2[i]
    mean = arraySum / n
 
    # Find sum of elements from array1
    # whose difference with mean
    # is not more than k
    sumOfElements = 0
    difference = 0
 
    for i in range(m):
 
        difference = arr1[i] - mean
 
        if ((difference < 0) and (k > (-1) * difference)):
            sumOfElements += arr1[i]
 
        if ((difference >= 0) and (k > difference)):
            sumOfElements += arr1[i]
 
    # Return result
    return sumOfElements
 
# Driver code
arr1 = [ 1, 2, 3, 4, 7, 9]
arr2 = [ 0, 1, 2, 1, 1, 4]
k = 2
 
m = len(arr1)
n = len(arr2)
 
print(findSumofEle(arr1, m, arr2, n, k))
 
# This code is contributed by mohit kumar

C#

// C# implementation of the approach
using System;
 
class GFG
{
     
// Function for finding sum of elements
// whose diff with mean is not more than k
static int findSumofEle(int []arr1, int m,
                int []arr2, int n, int k)
{
    float arraySum = 0;
 
    // Find the mean of second array
    for (int i = 0; i < n; i++)
        arraySum += arr2[i];
    float mean = arraySum / n;
 
    // Find sum of elements from array1
    // whose difference with mean in not more than k
    int sumOfElements = 0;
    float difference = 0;
 
    for (int i = 0; i < m; i++)
    {
        difference = arr1[i] - mean;
        if ((difference < 0) && (k > (-1) * difference))
        {
            sumOfElements += arr1[i];
        }
        if ((difference >= 0) && (k > difference))
        {
            sumOfElements += arr1[i];
        }
    }
 
    // Return result
    return sumOfElements;
}
 
// Driver code
static void Main()
{
    int []arr1 = { 1, 2, 3, 4, 7, 9 };
    int []arr2 = { 0, 1, 2, 1, 1, 4 };
    int k = 2;
 
    int m = arr1.Length;
    int n = arr2.Length;
 
    Console.WriteLine(findSumofEle(arr1, m, arr2, n, k));
}
}
 
// This code is contributed by mits

PHP

<?php
// PHP implementation of the approach
 
// Function for finding sum of elements
// whose diff with mean is not more than k
function findSumofEle($arr1, $m, $arr2, $n, $k)
{
    $arraySum = 0;
 
    // Find the mean of second array
    for ($i = 0; $i < $n; $i++)
        $arraySum += $arr2[$i];
         
    $mean = $arraySum / $n;
 
    // Find sum of elements from array1
    // whose difference with mean
    // is not more than k
    $sumOfElements = 0;
 
    for ($i = 0; $i < $m; $i++)
    {
        $difference = $arr1[$i] - $mean;
        if (($difference < 0) &&
            ($k > (-1) * $difference))
        {
            $sumOfElements += $arr1[$i];
        }
        if (($difference >= 0) &&
            ($k > $difference))
        {
            $sumOfElements += $arr1[$i];
        }
    }
 
    // Return result
    return $sumOfElements;
}
 
// Driver code
$arr1 = array( 1, 2, 3, 4, 7, 9 );
$arr2 = array( 0, 1, 2, 1, 1, 4 );
$k = 2;
 
$m = count($arr1);
$n = count($arr2);
 
print(findSumofEle($arr1, $m,
                   $arr2, $n, $k));
 
// This code is contributed by Ryuga
?>

Javascript

<script>
 
// Javascript implementation of the approach
 
// Function for finding sum of elements
// whose diff with mean is not more than k
function findSumofEle(arr1, m, arr2, n, k)
{
    var arraySum = 0;
 
    // Find the mean of second array
    for (var i = 0; i < n; i++)
        arraySum += arr2[i];
    var mean = (arraySum / n);
 
    // Find sum of elements from array1
    // whose difference with mean in not more than k
    var sumOfElements = 0;
    var difference;
 
    for (var i = 0; i < m; i++) {
        difference = arr1[i] - mean;
        if ((difference < 0) && (k > (-1) * difference)) {
            sumOfElements += arr1[i];
        }
        if ((difference >= 0) && (k > difference)) {
            sumOfElements += arr1[i];
        }
    }
 
    // Return result
    return sumOfElements;
}
 
// Driver code
var arr1 = [ 1, 2, 3, 4, 7, 9 ];
var arr2 = [ 0, 1, 2, 1, 1, 4 ];
var k = 2;
var m, n;
m = arr1.length;
n = arr2.length;
document.write( findSumofEle(arr1, m, arr2, n, k));
 
</script>
Producción: 

6

 

Tiempo Complejidad: O(n + m)
Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por Shivam.Pradhan 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 *