Recuento de sumas distintas que se pueden obtener sumando números primos de arrays dadas

Dadas dos arrays arr1[] y arr2[] . La tarea es contar las distintas sumas que se pueden obtener eligiendo un elemento primo de arr1[] y otro elemento primo de arr2[] .
Ejemplos: 
 

Entrada: arr1[] = {2, 3}, arr2[] = {2, 2, 4, 7} 
Salida:
Todos los pares primos posibles son (2, 2), (2, 2), (2, 7) , (3, 2), (3, 2) 
y (3, 7) con sumas 4, 4, 9, 5, 5 y 10 respectivamente.
Entrada: arr1[] = {3, 1, 4, 2, 5}, arr2[] = {8, 7, 10, 6, 5} 
Salida:
 

Enfoque: use el tamiz de Eratóstenes para verificar si un número es primo o no, luego, para cada par primo, almacene su suma en un conjunto para evitar duplicados. El tamaño del conjunto final será la respuesta requerida.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
#define MAX 1000000
using namespace std;
 
bool prime[MAX];
void sieve()
{
    memset(prime, true, sizeof(prime));
    prime[0] = prime[1] = false;
    for (int p = 2; p * p <= MAX; p++) {
        if (prime[p] == true) {
            for (int i = p * p; i <= MAX; i += p)
                prime[i] = false;
        }
    }
}
 
// Function to return the distinct sums
// that can be obtained by adding prime
// numbers from the given arrays
int distinctSum(int arr1[], int arr2[], int m, int n)
{
    sieve();
 
    // Set to store distinct sums
    set<int, greater<int> > sumSet;
 
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            if (prime[arr1[i]] && prime[arr2[j]])
                sumSet.insert(arr1[i] + arr2[j]);
 
    return sumSet.size();
}
 
// Driver code
int main()
{
    int arr1[] = { 2, 3 };
    int arr2[] = { 2, 2, 4, 7 };
    int m = sizeof(arr1) / sizeof(arr1[0]);
    int n = sizeof(arr2) / sizeof(arr2[0]);
    cout << distinctSum(arr1, arr2, m, n);
 
    return 0;
}

Java

// Java implementation of the approach
import java.util.*;
 
class GFG
{
static int MAX = 1000000;
 
static boolean []prime = new boolean[MAX + 1];
static void sieve()
{
    Arrays.fill(prime, true);
    prime[0] = prime[1] = false;
    for (int p = 2; p * p <= MAX; p++)
    {
        if (prime[p] == true)
        {
            for (int i = p * p;
                     i <= MAX; i += p)
                prime[i] = false;
        }
    }
}
 
// Function to return the distinct sums
// that can be obtained by adding prime
// numbers from the given arrays
static int distinctSum(int arr1[],
                       int arr2[],
                       int m, int n)
{
    sieve();
 
    // Set to store distinct sums
    Set<Integer> sumSet = new HashSet<Integer>();
 
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            if (prime[arr1[i]] && prime[arr2[j]])
                sumSet.add(arr1[i] + arr2[j]);
 
    return sumSet.size();
}
 
// Driver code
public static void main(String[] args)
{
    int arr1[] = { 2, 3 };
    int arr2[] = { 2, 2, 4, 7 };
    int m = arr1.length;
    int n = arr2.length;
    System.out.println(distinctSum(arr1, arr2, m, n));
}
}
 
// This code is contributed by Rajput-Ji

Python3

# Python3 implementation of the approach
MAX = 1000000
 
prime = [True for i in range(MAX + 1)]
 
def sieve():
 
    prime[0], prime[1] = False, False
 
    for p in range(2, MAX + 1):
        if p * p > MAX:
            break
        if (prime[p] == True):
            for i in range(2 * p, MAX + 1, p):
                prime[i] = False
 
# Function to return the distinct sums
# that can be obtained by adding prime
# numbers from the given arrays
def distinctSum(arr1, arr2, m, n):
    sieve()
 
    # Set to store distinct sums
    sumSet = dict()
 
    for i in range(m):
        for j in range(n):
            if (prime[arr1[i]] and
                prime[arr2[j]]):
                sumSet[arr1[i] + arr2[j]] = 1
 
    return len(sumSet)
 
# Driver code
arr1 = [2, 3 ]
arr2 = [2, 2, 4, 7 ]
m = len(arr1)
n = len(arr2)
print(distinctSum(arr1, arr2, m, n))
 
# This code is contributed by mohit kumar

C#

// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
static int MAX = 1000000;
 
static bool []prime = new bool[MAX + 1];
static void sieve()
{
    for (int i = 0; i < MAX + 1; i++)
        prime[i] = true;
    prime[0] = prime[1] = false;
    for (int p = 2; p * p <= MAX; p++)
    {
        if (prime[p] == true)
        {
            for (int i = p * p;
                     i <= MAX; i += p)
                prime[i] = false;
        }
    }
}
 
// Function to return the distinct sums
// that can be obtained by adding prime
// numbers from the given arrays
static int distinctSum(int []arr1,
                       int []arr2,
                       int m, int n)
{
    sieve();
 
    // Set to store distinct sums
    HashSet<int> sumSet = new HashSet<int>();
 
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            if (prime[arr1[i]] && prime[arr2[j]])
                sumSet.Add(arr1[i] + arr2[j]);
 
    return sumSet.Count;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr1 = { 2, 3 };
    int []arr2 = { 2, 2, 4, 7 };
    int m = arr1.Length;
    int n = arr2.Length;
    Console.WriteLine(distinctSum(arr1, arr2, m, n));
}
}
 
// This code is contributed by Rajput-Ji

Javascript

<script>
 
// JavaScript implementation of the approach
 
 
let MAX = 1000000
 
let prime = new Array(MAX);
 
function sieve() {
    prime.fill(true)
    prime[0] = prime[1] = false;
    for (let p = 2; p * p <= MAX; p++) {
        if (prime[p] == true) {
            for (let i = p * p; i <= MAX; i += p)
                prime[i] = false;
        }
    }
}
 
// Function to return the distinct sums
// that can be obtained by adding prime
// numbers from the given arrays
function distinctSum(arr1, arr2, m, n) {
    sieve();
 
    // Set to store distinct sums
    let sumSet = new Set();
 
    for (let i = 0; i < m; i++)
        for (let j = 0; j < n; j++)
            if (prime[arr1[i]] && prime[arr2[j]])
                sumSet.add(arr1[i] + arr2[j]);
 
    return sumSet.size;
}
 
// Driver code
 
let arr1 = [2, 3];
let arr2 = [2, 2, 4, 7];
let m = arr1.length;
let n = arr2.length;
document.write(distinctSum(arr1, arr2, m, n));
 
// This code is contributed by _saurabh_jaiswal
 
</script>
Producción: 

4

 

Complejidad de tiempo: O (N * M * log (N * M) + MAX * log (MAX))

Espacio Auxiliar: O(MAX + N * M) 

Publicación traducida automáticamente

Artículo escrito por namankhare42 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 *