# 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 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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Time Complexity:** O(1)

## Recommended Posts:

- Check if N can be represented as sum of integers chosen from set {A, B}
- Check if the sum of distinct digits of two integers are equal
- Check if a number can be written as sum of three consecutive integers
- Check whether product of integers from a to b is positive , negative or zero
- Check if array contains contiguous integers with duplicates allowed
- Check whether a number can be represented as sum of K distinct positive integers
- QA - Placement Quizzes | Ratio and Proportion | Question 7
- QA - Placement Quizzes | Ratio and Proportion | Question 9
- QA - Placement Quizzes | Ratio and Proportion | Question 6
- QA - Placement Quizzes | Ratio and Proportion | Question 1
- QA - Placement Quizzes | Ratio and Proportion | Question 8
- QA - Placement Quizzes | Ratio and Proportion | Question 5
- QA - Placement Quizzes | Ratio and Proportion | Question 3
- QA - Placement Quizzes | Ratio and Proportion | Question 4
- QA - Placement Quizzes | Ratio and Proportion | Question 2

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.