Verifique si dos arrays son permutaciones entre sí usando la operación matemática

Dadas dos arrays no ordenadas del mismo tamaño donde arr[i] >= 0 para todo i, la tarea es verificar si dos arrays son permutaciones entre sí o no.
Ejemplos: 
 

Input: arr1[] = {2, 1, 3, 5, 4, 3, 2}
       arr2[] = {3, 2, 2, 4, 5, 3, 1}
Output: Yes

Input: arr1[] = {2, 1, 3, 5}
       arr2[] = {3, 2, 2, 4}
Output: No

Ya se ha discutido en Verificar si dos arreglos son permutaciones entre sí usando Sorting and Hashing . Pero en esta publicación, se discute un enfoque diferente.
 

Acercarse:  

  1. Recorra la primera array A, sume y multiplique todos los elementos y guárdelos en variables como Sum1 y Mul1 respectivamente.
  2. De manera similar, recorra la segunda array B, sume y multiplique todos los elementos y guárdelos en variables como Sum2 y Mul2 respectivamente.
  3. Ahora, compare sum1, sum2 y mul1, mul2. Si Sum1 == Sum2 y Mul1 == Mul2 , entonces ambas arrays son permutaciones entre sí, de lo contrario no.

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

C++

// CPP code to check if arrays
// are permutations of eah other
#include <iostream>
using namespace std;
 
// Function to check if arrays
// are permutations of each other.
bool arePermutations(int a[], int b[], int n, int m)
{
 
    int sum1 = 0, sum2 = 0, mul1 = 1, mul2 = 1;
 
    // Calculating sum and multiply of first array
    for (int i = 0; i < n; i++) {
        sum1 += a[i];
        mul1 *= a[i];
    }
 
    // Calculating sum and multiply of second array
    for (int i = 0; i < m; i++) {
        sum2 += b[i];
        mul2 *= b[i];
    }
 
    // If sum and mul of both arrays are equal,
    // return true, else return false.
    return ((sum1 == sum2) && (mul1 == mul2));
}
 
// Driver code
int main()
{
 
    int a[] = { 1, 3, 2 };
    int b[] = { 3, 1, 2 };
 
    int n = sizeof(a) / sizeof(int);
    int m = sizeof(b) / sizeof(int);
 
    if (arePermutations(a, b, n, m))
        cout << "Yes" << endl;
     
    else
        cout << "No" << endl;
 
    return 0;
}

Java

// Java code to check if arrays
// are permutations of eah other
 
import java.io.*;
 
class GFG {
 
 
// Function to check if arrays
// are permutations of each other.
static boolean arePermutations(int a[], int b[], int n, int m)
{
 
    int sum1 = 0, sum2 = 0, mul1 = 1, mul2 = 1;
 
    // Calculating sum and multiply of first array
    for (int i = 0; i < n; i++) {
        sum1 += a[i];
        mul1 *= a[i];
    }
 
    // Calculating sum and multiply of second array
    for (int i = 0; i < m; i++) {
        sum2 += b[i];
        mul2 *= b[i];
    }
 
    // If sum and mul of both arrays are equal,
    // return true, else return false.
    return ((sum1 == sum2) && (mul1 == mul2));
}
 
// Driver code
 
    public static void main (String[] args) {
            int a[] = { 1, 3, 2 };
    int b[] = { 3, 1, 2 };
 
    int n = a.length;
    int m = b.length;
 
    if (arePermutations(a, b, n, m)==true)
        System.out.println( "Yes");
     
    else
        System.out.println( "No");
    }
}
// This code is contributed by  inder_verma..

Python3

# Python 3 program to check if arrays
# are permutations of eah other
 
# Function to check if arrays
# are permutations of each other
def arePermutations(a, b, n, m) :
 
    sum1, sum2, mul1, mul2 = 0, 0, 1, 1
 
    # Calculating sum and multiply of first array
    for i in range(n) :
        sum1 += a[i]
        mul1 *= a[i]
 
    # Calculating sum and multiply of second array
    for i in range(m) :
        sum2 += b[i]
        mul2 *= b[i]
 
    # If sum and mul of both arrays are equal,
    # return true, else return false.
    return((sum1 == sum2) and (mul1 == mul2))
 
 
# Driver code    
if __name__ == "__main__" :
 
    a = [ 1, 3, 2]
    b = [ 3, 1, 2]
 
    n = len(a)
    m = len(b)
 
    if arePermutations(a, b, n, m) :
        print("Yes")
 
    else :
        print("No")
 
  
# This code is contributed by ANKITRAI1

C#

// C# code to check if arrays
// are permutations of eah other
using System;
 
class GFG
{
 
// Function to check if arrays
// are permutations of each other.
static bool arePermutations(int[] a, int[] b,
                            int n, int m)
{
 
    int sum1 = 0, sum2 = 0,
        mul1 = 1, mul2 = 1;
 
    // Calculating sum and multiply
    // of first array
    for (int i = 0; i < n; i++)
    {
        sum1 += a[i];
        mul1 *= a[i];
    }
 
    // Calculating sum and multiply
    // of second array
    for (int i = 0; i < m; i++)
    {
        sum2 += b[i];
        mul2 *= b[i];
    }
 
    // If sum and mul of both arrays
    // are equal, return true, else
    // return false.
    return ((sum1 == sum2) &&
            (mul1 == mul2));
}
 
// Driver code
public static void Main ()
{
    int[] a = { 1, 3, 2 };
    int[] b = { 3, 1, 2 };
     
    int n = a.Length;
    int m = b.Length;
     
    if (arePermutations(a, b, n, m) == true)
        Console.Write( "Yes");
     
    else
        Console.Write( "No");
}
}
 
// This code is contributed
// by ChitraNayal

PHP

<?php
// PHP code to check if arrays
// are permutations of eah other
 
// Function to check if arrays
// are permutations of each other.
function arePermutations($a, $b, $n, $m)
{
 
    $sum1 = 0; $sum2 = 0;
    $mul1 = 1; $mul2 = 1;
 
    // Calculating sum and multiply
    // of first array
    for ($i = 0; $i < $n; $i++)
    {
        $sum1 += $a[$i];
        $mul1 *= $a[$i];
    }
 
    // Calculating sum and multiply
    // of second array
    for ($i = 0; $i < $m; $i++)
    {
        $sum2 += $b[$i];
        $mul2 *= $b[$i];
    }
 
    // If sum and mul of both arrays
    // are equal, return true, else
    // return false.
    return (($sum1 == $sum2) &&
            ($mul1 == $mul2));
}
 
// Driver code
$a = array( 1, 3, 2 );
$b = array( 3, 1, 2 );
 
$n = sizeof($a);
$m = sizeof($b);
 
if (arePermutations($a, $b, $n, $m))
    echo "Yes" . "\n";
else
    echo "No" . "\n";
 
// This code is contributed
// by Akanksha Rai(Abby_akku)

Javascript

<script>
 
// JavaScript code to check if arrays
// are permutations of eah other
     
    // Function to check if arrays
   // are permutations of each other.
    function arePermutations(a,b,n,m)
    {
        let sum1 = 0, sum2 = 0, mul1 = 1, mul2 = 1;
   
    // Calculating sum and multiply of first array
    for (let i = 0; i < n; i++) {
        sum1 += a[i];
        mul1 *= a[i];
    }
   
    // Calculating sum and multiply of second array
    for (let i = 0; i < m; i++) {
        sum2 += b[i];
        mul2 *= b[i];
    }
   
    // If sum and mul of both arrays are equal,
    // return true, else return false.
    return ((sum1 == sum2) && (mul1 == mul2));
     
    }
     
    // Driver code
    let a=[1, 3, 2 ];
    let b=[3, 1, 2];
     
    let n = a.length;
    let m = b.length;
     
    if (arePermutations(a, b, n, m)==true)
        document.write( "Yes");
       
    else
        document.write( "No");
     
 
// This code is contributed by rag2127
 
</script>
Producción: 

Yes

 

Publicación traducida automáticamente

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