Contar sumas de pares que son factores de la suma de la array

Dada una array arr[] que consta de N enteros, la tarea es encontrar el número de pares, donde i ≤ j , tal que la suma de pares divida la suma de los elementos de la array .

Ejemplos:

Entrada: arr[] = {1, 2, 3, 4, 5}
Salida: 3
Explicación:
A continuación se muestran los pares que dividen la suma de la array (= 1 + 2 + 3 + 4 + 5 = 15):

  1. (1, 2): Suma de pares = 1 + 2 = 3, que divide suma(= 15).
  2. (1, 4): Suma de pares = 1 + 4 = 5, que divide suma(= 15).
  3. (2, 3): Suma de pares = 2 + 3 = 5, que divide suma(= 15).

Por lo tanto, la cuenta de pares es 3.

Entrada: arr[] = {1, 5, 2}
Salida: 0

Enfoque: El problema dado se puede resolver generando todos los pares posibles (arr[i], arr[j]) a partir del arreglo dado tal que i ≤ j y contar todos esos pares cuya suma de elementos divide la suma del arreglo . Después de verificar todos los pares posibles, imprima el conteo total obtenido.

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

C++

// C++ program for the above approach
 
#include <iostream>
using namespace std;
 
// Function to find the number of pairs
// whose sums divides the sum of array
void countPairs(int arr[], int N)
{
    // Initialize the totalSum and
    // count as 0
    int count = 0, totalSum = 0;
 
    // Calculate the total sum of array
    for (int i = 0; i < N; i++) {
        totalSum += arr[i];
    }
 
    // Generate all possible pairs
    for (int i = 0; i < N; i++) {
 
        for (int j = i + 1; j < N; j++) {
 
            // If the sum is a factor
            // of totalSum or not
            if (totalSum
                    % (arr[i] + arr[j])
                == 0) {
 
                // Increment count by 1
                count += 1;
            }
        }
    }
 
    // Print the total count obtained
    cout << count;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    countPairs(arr, N);
 
    return 0;
}

Java

// Java program for the above approach
import java.io.*;
 
class GFG
{
   
    // Function to find the number of pairs
    // whose sums divides the sum of array
    public static void countPairs(int arr[], int N)
    {
       
        // Initialize the totalSum and
        // count as 0
        int count = 0, totalSum = 0;
 
        // Calculate the total sum of array
        for (int i = 0; i < N; i++)
        {
            totalSum += arr[i];
        }
 
        // Generate all possible pairs
        for (int i = 0; i < N; i++) {
 
            for (int j = i + 1; j < N; j++) {
 
                // If the sum is a factor
                // of totalSum or not
                if (totalSum % (arr[i] + arr[j]) == 0) {
 
                    // Increment count by 1
                    count += 1;
                }
            }
        }
 
        // Print the total count obtained
        System.out.println(count);
    }
 
  // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 1, 2, 3, 4, 5 };
        int N = arr.length;
        countPairs(arr, N);
      
    }
}
 
 // This code is contributed by Potta Lokesh

Python3

# Python3 program for the above approach
 
# Function to find the number of pairs
# whose sums divides the sum of array
def countPairs(arr, N):
     
    # Initialize the totalSum and
    # count as 0
    count = 0
    totalSum = 0
 
    # Calculate the total sum of array
    for i in range(N):
        totalSum += arr[i]
 
    # Generate all possible pairs
    for i in range(N):
        for j in range(i + 1, N, 1):
             
            # If the sum is a factor
            # of totalSum or not
            if (totalSum % (arr[i] + arr[j]) == 0):
                 
                # Increment count by 1
                count += 1
 
    # Print the total count obtained
    print(count)
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 2, 3, 4, 5 ]
    N = len(arr)
     
    countPairs(arr, N)
     
# This code is contributed by SURENDRA_GANGWAR

C#

// C# program for the above approach
using System;
 
class GFG{
     
    // Function to find the number of pairs
    // whose sums divides the sum of array
    public static void countPairs(int[] arr, int N)
    {
       
        // Initialize the totalSum and
        // count as 0
        int count = 0, totalSum = 0;
 
        // Calculate the total sum of array
        for (int i = 0; i < N; i++)
        {
            totalSum += arr[i];
        }
 
        // Generate all possible pairs
        for (int i = 0; i < N; i++) {
 
            for (int j = i + 1; j < N; j++) {
 
                // If the sum is a factor
                // of totalSum or not
                if (totalSum % (arr[i] + arr[j]) == 0) {
 
                    // Increment count by 1
                    count += 1;
                }
            }
        }
 
        // Print the total count obtained
        Console.WriteLine(count);
    }
 
// Driver code
static public void Main()
{
    int[] arr = { 1, 2, 3, 4, 5 };
        int N = arr.Length;
        countPairs(arr, N);
}
}
 
// This code is contributed by splevel62.

Javascript

<script>
        // JavaScript program for the above approach
 
        // Function to find the number of pairs
        // whose sums divides the sum of array
        function countPairs(arr, N)
        {
         
            // Initialize the totalSum and
            // count as 0
            let count = 0, totalSum = 0;
 
            // Calculate the total sum of array
            for (let i = 0; i < N; i++) {
                totalSum += arr[i];
            }
 
            // Generate all possible pairs
            for (let i = 0; i < N; i++)
            {
                for (let j = i + 1; j < N; j++)
                {
 
                    // If the sum is a factor
                    // of totalSum or not
                    if (totalSum
                        % (arr[i] + arr[j])
                        == 0) {
 
                        // Increment count by 1
                        count += 1;
                    }
                }
            }
 
            // Print the total count obtained
            document.write(count);
        }
 
        // Driver Code
        let arr = [1, 2, 3, 4, 5];
        let N = arr.length;
        countPairs(arr, N);
 
// This code is contributed by Potta Lokesh
    </script>
Producción: 

3

 

Complejidad de Tiempo: O(N 2 )
Espacio Auxiliar: O(1)

Publicación traducida automáticamente

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