Skip to content
Related Articles
Find pair with maximum ratio in an Array
• Last Updated : 20 Apr, 2021

Given an array arr[], the task is to find the maximum ratio pair in the array.
Examples:

Input: arr[] = { 15, 10, 3 }
Output:
Explanation:
Maximum ratio pair will be – Input: arr[] = { 15, 10, 3, 2 }
Output: 7.5
Explanation:
Maximum ratio pair will be – Approach: The idea is to iterate over every possible pair of the array using two nested loops and find the maximum ratio pair possible. For any pair, the maximum ratio can be obtained using Below is the implementation of the above approach:

## C++

 // C++ implementation to find// the maximum pair in the array #include  using namespace std; // Function to find the maximum pair// possible for the arrayfloat computeMaxValue(float arr[], int n){    float ans = 0;     // Loop to iterate over every    // possible pair in the array    for (int i = 0; i < n - 1; i++) {        for (int j = i + 1; j < n; j++) {             // Check pair (x, y) as well as            // (y, x) for maximum value            float val = max(arr[i] / arr[j],                            arr[j] / arr[i]);             // Update the answer            ans = max(ans, val);        }    }    return ans;} // Driver Codeint main(){    float arr[] = { 15, 10, 3, 2 };    int n = sizeof(arr) / sizeof(arr);    cout << computeMaxValue(arr, n);    return 0;}

## Java

 // Java implementation to find// the maximum pair in the arrayimport java.io.*;import java.util.*; class GFG {     // Function to find the maximum pair// possible for the arraystatic float computeMaxValue(float arr[], int n){    float ans = 0;         // Loop to iterate over every    // possible pair in the array    for(int i = 0; i < n - 1; i++)    {       for(int j = i + 1; j < n; j++)       {                     // Check pair (x, y) as well as          // (y, x) for maximum value          float val = Math.max(arr[i] / arr[j],                               arr[j] / arr[i]);           // Update the answer          ans = Math.max(ans, val);       }    }         return ans;} // Driver codepublic static void main(String[] args){    float arr[] = { 15, 10, 3, 2 };    int N = arr.length;         System.out.println(computeMaxValue(arr, N));}} // This code is contributed by coder001

## Python3

 # Python3 implementation to find# the maximum pair in the array # Function to find the maximum pair# possible for the arraydef computeMaxValue(arr, n):     ans = 0     # Loop to iterate over every    # possible pair in the array    for i in range(n - 1):        for j in range(i + 1, n):             # Check pair (x, y) as well as            # (y, x) for maximum value            val = max(arr[i] / arr[j],                      arr[j] / arr[i])             # Update the answer            ans = max(ans, val)                 return ans # Driver Codeif __name__ == "__main__":         arr = [ 15, 10, 3, 2 ]    n = len(arr)     print(computeMaxValue(arr, n))     # This code is contributed by chitranayal

## C#

 // C# implementation to find// the maximum pair in the arrayusing System; class GFG {     // Function to find the maximum pair// possible for the arraystatic float computeMaxValue(float []arr, int n){    float ans = 0;         // Loop to iterate over every    // possible pair in the array    for(int i = 0; i < n - 1; i++)    {       for(int j = i + 1; j < n; j++)       {                     // Check pair (x, y) as well as          // (y, x) for maximum value          float val = Math.Max(arr[i] / arr[j],                               arr[j] / arr[i]);                     // Update the answer          ans = Math.Max(ans, val);       }    }    return ans;} // Driver codepublic static void Main(String[] args){    float []arr = { 15, 10, 3, 2 };    int N = arr.Length;         Console.WriteLine(computeMaxValue(arr, N));}} // This code is contributed by 29AjayKumar

## Javascript

 
Output:
7.5

Time Complexity: O(N2)

Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live

My Personal Notes arrow_drop_up