Comprueba si los enteros dados a, b, c y d están en proporción

Dados cuatro enteros a , b , c y d . La tarea es verificar si es posible emparejarlos de manera que estén en proporción. Se nos permite barajar el orden de los números.
Ejemplos: 
 

Entrada: arr[] = {1, 2, 4, 2} 
Salida: Sí 
1/2 = 2/4
Entrada: arr[] = {1, 2, 5, 2} 
Salida: No 
 

Enfoque: si cuatro números a, b, c y d están en proporción, entonces a:b = c:d . La solución es ordenar los cuatro números y emparejar los 2 primeros juntos y los 2 últimos juntos y verificar sus proporciones esto se debe a que, para que estén en proporción, el producto de los medios tiene que ser igual al producto de los extremos. . Entonces, a * d tiene que ser igual a c * b .
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function that returns true if the
// given four integers are in proportion
bool inProportion(int arr[])
{
 
    // Array will consist of
    // only four integers
    int n = 4;
 
    // Sort the array
    sort(arr, arr + n);
 
    // Find the product of extremes and means
    long extremes = (long)arr[0] * (long)arr[3];
    long means = (long)arr[1] * (long)arr[2];
 
    // If the products are equal
    if (extremes == means)
        return true;
    return false;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 4, 2 };
 
    if (inProportion(arr))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}

Java

// Java implementation of the approach
import java.util.*;
 
class GFG
{
     
// Function that returns true if the
// given four integers are in proportion
static boolean inProportion(int []arr)
{
 
    // Array will consist of
    // only four integers
    int n = 4;
 
    // Sort the array
    Arrays.sort(arr);
 
    // Find the product of extremes and means
    long extremes = (long)arr[0] * (long)arr[3];
    long means = (long)arr[1] * (long)arr[2];
 
    // If the products are equal
    if (extremes == means)
        return true;
    return false;
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 1, 2, 4, 2 };
 
    if (inProportion(arr))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by Rajput-Ji

Python3

# Python3 implementation of the approach
 
# Function that returns true if the
# given four integers are in proportion
def inProportion(arr) :
 
    # Array will consist of
    # only four integers
    n = 4;
 
    # Sort the array
    arr.sort()
 
    # Find the product of extremes and means
    extremes = arr[0] * arr[3];
    means = arr[1] * arr[2];
 
    # If the products are equal
    if (extremes == means) :
        return True;
         
    return False;
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 1, 2, 4, 2 ];
 
    if (inProportion(arr)) :
        print("Yes");
    else :
        print("No");
 
# This code is contributed by AnkitRai01

C#

// C# implementation of the approach
using System;
                     
class GFG
{
     
// Function that returns true if the
// given four integers are in proportion
static bool inProportion(int []arr)
{
 
    // Array will consist of
    // only four integers
    int n = 4;
 
    // Sort the array
    Array.Sort(arr);
 
    // Find the product of extremes and means
    long extremes = (long)arr[0] * (long)arr[3];
    long means = (long)arr[1] * (long)arr[2];
 
    // If the products are equal
    if (extremes == means)
        return true;
    return false;
}
 
// Driver code
public static void Main(String []args)
{
    int []arr = { 1, 2, 4, 2 };
 
    if (inProportion(arr))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed by Princi Singh

Javascript

<script>
 
// Javascript implementation of the approach
 
// Function that returns true if the
// given four integers are in proportion
function inProportion(arr)
{
 
    // Array will consist of
    // only four integers
    var n = 4;
 
    // Sort the array
    arr.sort();
 
    // Find the product of extremes and means
    var extremes = arr[0] * arr[3];
    var means = arr[1] * arr[2];
 
    // If the products are equal
    if (extremes == means)
        return true;
    return false;
}
 
// Driver code
var arr = [ 1, 2, 4, 2 ]
if (inProportion(arr))
    document.write("Yes");
else
    document.write("No");
 
// This code is contributed by rutvik_56.
</script>
Producción: 

Yes

 

Complejidad de tiempo: O(n * log n)
 

Publicación traducida automáticamente

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