Find pair with maximum ratio in an Array

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

Examples:

Input: arr[] = { 15, 10, 3 }
Output: 5
Explanation:
Maximum ratio pair will be – \frac{15}{3} = 5

Input: arr[] = { 15, 10, 3, 2 }
Output: 7.5
Explanation:
Maximum ratio pair will be – \frac{15}{2} = 7.5

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 \frac{max(arr_i, arr_j)}{min(arr_i, arr_j)}



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find
// the maximum pair in the array
  
#include <bits/stdc++.h>
  
using namespace std;
  
// Function to find the maximum pair
// possible for the array
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 = max(arr[i] / arr[j],
                            arr[j] / arr[i]);
  
            // Update the answer
            ans = max(ans, val);
        }
    }
    return ans;
}
  
// Driver Code
int main()
{
    float arr[] = { 15, 10, 3, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << computeMaxValue(arr, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to find 
// the maximum pair in the array 
import java.io.*; 
import java.util.*; 
  
class GFG { 
      
// Function to find the maximum pair 
// possible for the array 
static 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 code 
public 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to find
# the maximum pair in the array
  
# Function to find the maximum pair
# possible for the array
def 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 Code
if __name__ == "__main__":
      
    arr = [ 15, 10, 3, 2 ]
    n = len(arr)
  
    print(computeMaxValue(arr, n))
      
# This code is contributed by chitranayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to find 
// the maximum pair in the array 
using System;
  
class GFG { 
      
// Function to find the maximum pair 
// possible for the array 
static 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 code 
public 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

chevron_right


Output:

7.5

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.