Check whether the given integers a, b, c and d are in proportion

Given four integers a, b, c and d. The task is to check whether it is possible to pair them up such that they are in proportion. We are allowed to shuffle the order of the numbers.
Examples:Â
Â
Input: arr[] = {1, 2, 4, 2}Â
Output: YesÂ
1 / 2 = 2 / 4
Input: arr[] = {1, 2, 5, 2}Â
Output: NoÂ
Â
Â
Approach: If four numbers a, b, c and d are in proportion then a:b = c:d. The solution is to sort the four numbers and pair up the first 2 together and the last 2 together and check their ratios this is because, in order for them to be in proportion, the product of means has to be equal to the product of extremes. So, a * d has to be equal to c * b.
Below is the implementation of the above approach:Â
Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function that returns true if the// given four integers are in proportionbool 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 codeint main(){Â Â Â Â int arr[] = { 1, 2, 4, 2 };Â
    if (inProportion(arr))        cout << "Yes";    else        cout << "No";Â
    return 0;} |
Java
// Java implementation of the approachimport java.util.*;Â
class GFG{Â Â Â Â Â // Function that returns true if the// given four integers are in proportionstatic 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 codepublic 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 approachusing System;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â class GFG{Â Â Â Â Â // Function that returns true if the// given four integers are in proportionstatic 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 codepublic 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 proportionfunction 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 codevar arr = [ 1, 2, 4, 2 ]if (inProportion(arr))    document.write("Yes");else    document.write("No");Â
// This code is contributed by rutvik_56.</script> |
Yes
Â
Time Complexity: O(n * log n)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



