Cuente el número de pares (i, j) tales que arr[i] * arr[j] = arr[i] + arr[j]

Dada una array arr[] de longitud N, cuente el número de pares (i, j) tales que arr[i] * arr[j] = arr[i] + arr[j] y 0 <= i < j <= n _ También se da que los elementos de la array pueden ser cualquier número entero positivo, incluido el cero.

Ejemplos:

Input : arr[] = {2, 0, 3, 2, 0} 
Output : 2

Input : arr[] = {1, 2, 3, 4}
Output : 0

Solución simple:
podemos generar todos los pares posibles de la array y contar aquellos pares que satisfacen la condición dada.

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

CPP

// C++ program to count pairs (i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the count of pairs(i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
long countPairs(int arr[], int n)
{
    long count = 0;
 
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
 
            // Increment count if condition satisfy
            if (arr[i] * arr[j] == arr[i] + arr[j])
                count++;
        }
    }
 
    // Return count of pairs
    return count;
}
 
// Driver code
int main()
{
 
    int arr[] = { 2, 0, 3, 2, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Get and print count of pairs
    cout << countPairs(arr, n);
 
    return 0;
}

Java

// Java program to count pairs (i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
 
class GFG {
    // Function to return the count of pairs(i, j)
    // such that arr[i] * arr[j] = arr[i] + arr[j]
    static long countPairs(int arr[], int n)
    {
        long count = 0;
 
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
 
                // Increment count if condition satisfy
                if (arr[i] * arr[j] == arr[i] + arr[j])
                    count++;
            }
        }
 
        // Return count of pairs
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        int arr[] = { 2, 0, 3, 2, 0 };
        int n = arr.length;
 
        // Get and print count of pairs
        System.out.println(countPairs(arr, n));
    }
}

Python3

# Python3 program to count pairs (i, j)
# such that arr[i] * arr[j] = arr[i] + arr[j]
 
# Function to return the count of pairs(i, j)
# such that arr[i] * arr[j] = arr[i] + arr[j]
def countPairs(arr, n) :
 
    count = 0;
 
    for i in range(n - 1) :
        for j in range(i + 1, n) :
 
            # Increment count if condition satisfy
            if (arr[i] * arr[j] == arr[i] + arr[j]) :
                count += 1;
 
    # Return count of pairs
    return count;
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 2, 0, 3, 2, 0 ];
    n = len(arr);
 
    # Get and print count of pairs
    print(countPairs(arr, n));
     
# This code is contributed by AnkitRai01

C#

// C# program to count pairs (i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
 
using System;
class GFG {
    // Function to return the count of pairs(i, j)
    // such that arr[i] * arr[j] = arr[i] + arr[j]
    static long countPairs(int[] arr, int n)
    {
        long count = 0;
 
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
 
                // Increment count if condition satisfy
                if (arr[i] * arr[j] == arr[i] + arr[j])
                    count++;
            }
        }
 
        // Return count of pairs
        return count;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
 
        int[] arr = { 2, 0, 3, 2, 0 };
        int n = arr.Length;
 
        // Get and print count of pairs
        Console.WriteLine(countPairs(arr, n));
    }
}

Javascript

<script>
// Function to return the count of pairs(i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
function countPairs(arr, n)
{
let count = 0;
 
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
 
 
// Increment count if condition satisfy
if (arr[i] * arr[j] == arr[i] + arr[j])
count++;
}
}
 
// Return count of pairs
return count;
}
 
 
let arr = [ 2, 0, 3, 2, 0 ];
let n = arr.length;
 
document.write(countPairs(arr, n));
// This code is contributed by khatriharsh281</script>

CPP

// C++ program to count pairs (i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the count of pairs(i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
long countPairs(int arr[], int n)
{
 
    int countZero = 0;
    int countTwo = 0;
 
    // Count number of 0's and 2's in the array
    for (int i = 0; i < n; i++) {
        if (arr[i] == 0)
            countZero++;
 
        else if (arr[i] == 2)
            countTwo++;
    }
 
    // Total pairs due to occurrence of 0's
    long pair0 = (countZero * (countZero - 1)) / 2;
 
    // Total pairs due to occurrence of 2's
    long pair2 = (countTwo * (countTwo - 1)) / 2;
 
    // Return count of all pairs
    return pair0 + pair2;
}
 
// Driver code
int main()
{
 
    int arr[] = { 2, 0, 3, 2, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Get and print count of pairs
    cout << countPairs(arr, n);
 
    return 0;
}

Java

// Java program to count pairs (i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
 
class GFG {
    // Function to return the count of pairs(i, j)
    // such that arr[i] * arr[j] = arr[i] + arr[j]
    static long countPairs(int arr[], int n)
    {
 
        int countZero = 0;
        int countTwo = 0;
 
        // Count number of 0's and 2's in the array
        for (int i = 0; i < n; i++) {
            if (arr[i] == 0)
                countZero++;
 
            else if (arr[i] == 2)
                countTwo++;
        }
 
        // Total pairs due to occurrence of 0's
        long pair0 = (countZero * (countZero - 1)) / 2;
 
        // Total pairs due to occurrence of 2's
        long pair2 = (countTwo * (countTwo - 1)) / 2;
 
        // Return count of all pairs
        return pair0 + pair2;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        int arr[] = { 2, 0, 3, 2, 0 };
        int n = arr.length;
 
        // Get and print count of pairs
        System.out.println(countPairs(arr, n));
    }
}

Python3

# Python3 program to count pairs (i, j)
# such that arr[i] * arr[j] = arr[i] + arr[j]
 
# Function to return the count of pairs(i, j)
# such that arr[i] * arr[j] = arr[i] + arr[j]
def countPairs(arr, n):
 
    countZero = 0;
    countTwo = 0;
 
    # Count number of 0's and 2's in the array
    for i in range(n) :
        if (arr[i] == 0) :
            countZero += 1;
 
        elif (arr[i] == 2) :
            countTwo += 1;
 
    # Total pairs due to occurrence of 0's
    pair0 = (countZero * (countZero - 1)) // 2;
 
    # Total pairs due to occurrence of 2's
    pair2 = (countTwo * (countTwo - 1)) // 2;
 
    # Return count of all pairs
    return pair0 + pair2;
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 2, 0, 3, 2, 0 ];
    n = len(arr);
 
    # Get and print count of pairs
    print(countPairs(arr, n));
 
# This code is contributed by AnkitRai01

C#

// C# program to count pairs (i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
 
using System;
class GFG {
    // Function to return the count of pairs(i, j)
    // such that arr[i] * arr[j] = arr[i] + arr[j]
    static long countPairs(int[] arr, int n)
    {
 
        int countZero = 0;
        int countTwo = 0;
 
        // Count number of 0's and 2's in the array
        for (int i = 0; i < n; i++) {
            if (arr[i] == 0)
                countZero++;
 
            else if (arr[i] == 2)
                countTwo++;
        }
 
        // Total pairs due to occurrence of 0's
        long pair0 = (countZero * (countZero - 1)) / 2;
 
        // Total pairs due to occurrence of 2's
        long pair2 = (countTwo * (countTwo - 1)) / 2;
 
        // Return count of all pairs
        return pair0 + pair2;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
 
        int[] arr = { 2, 0, 3, 2, 0 };
        int n = arr.Length;
 
        // Get and print count of pairs
        Console.WriteLine(countPairs(arr, n));
    }
}

Publicación traducida automáticamente

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