Open In App

Find pair with maximum ratio in an Array

Improve
Improve
Like Article
Like
Save
Share
Report

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 – \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++

// 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;
}

                    

Java

// 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

                    

Python3

# 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

                    

C#

// 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

                    

Javascript

<script>
 
// Javascript implementation to find
// the maximum pair in the array
 
// Function to find the maximum pair
// possible for the array
function computeMaxValue(arr, n)
{
    var ans = 0;
 
    // Loop to iterate over every
    // possible pair in the array
    for (var i = 0; i < n - 1; i++) {
        for (var j = i + 1; j < n; j++) {
 
            // Check pair (x, y) as well as
            // (y, x) for maximum value
            var val = Math.max(arr[i] / arr[j],
                            arr[j] / arr[i]);
 
            // Update the answer
            ans = Math.max(ans, val);
        }
    }
    return ans;
}
 
// Driver Code
var arr = [ 15, 10, 3, 2 ];
var n = arr.length;
document.write( computeMaxValue(arr, n));
 
</script>

                    

Output
7.5

Time Complexity: O(N2)

Auxiliary Space: O(1)

Approach-2  For a[i]>0

As the maximum ratio can be obtained when the denominator is minimum. 

  1. So store the index of the minimum element of the array.
  2. Loop through the array and divide each integer with the minimum number.
  3. Maintain a running maximum.

Below is the implementation of the above approach:  

C++

// C++ implementation to find
// the maximum pair in the array
 
#include <bits/stdc++.h>
 
using namespace std;
 
// Function to find the index of minimum number of array
 
int minNum(float a[], int n)
{
    int indOfMin = 0; // initializing with 0
 
    for (int i = 0; i < n; i++) {
        if (a[i] < a[indOfMin])
            indOfMin = i;
    }
    return indOfMin;
}
 
// Function to find the maximum pair
// possible for the array
float computeMaxValue(float a[], int n)
{
    int minIndex = minNum(a, n);
 
    float ans = INT_MIN;
 
    for (int i = 0; i < n; i++) {
        if (i != minIndex) {
            float temp = float(a[i]) / float(a[minIndex]);
            ans = max(ans, temp);
        }
    }
 
    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;
}
 
// This code is contributed by Palak Gupta

                    

Java

// Java implementation to find
// the maximum pair in the array
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find the index of minimum number of array
 
    static int minNum(float a[], int n)
    {
        int indOfMin = 0; // initializing with 0
 
        for (int i = 0; i < n; i++) {
            if (a[i] < a[indOfMin])
                indOfMin = i;
        }
        return indOfMin;
    }
 
    // Function to find the maximum pair
    // possible for the array
    static float computeMaxValue(float a[], int n)
    {
        int minIndex = minNum(a, n);
 
        float ans = Integer.MIN_VALUE;
 
        for (int i = 0; i < n; i++) {
            if (i != minIndex) {
                float temp
                    = (float)a[i] / (float)a[minIndex];
                if (temp > ans)
                    ans = temp;
            }
        }
 
        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 Palak Gupta

                    

Python3

# Python implementation to find
# the maximum pair in the array
 
# Function to find the index of minimum number of array
def minNum(a,  n):
    indOfMin = 0  # initializing with 0
 
    for i in range(0, n):
        if (a[i] < a[indOfMin]):
            indOfMin = i
 
    return indOfMin
 
# Function to find the maximum pair
# possible for the array
def computeMaxValue(a, n):
    minIndex = minNum(a, n)
 
    ans = float('-inf')
 
    for i in range(0, n):
        if (i != minIndex):
            temp = float(a[i]) / float(a[minIndex])
            ans = max(ans, temp)
    return ans
 
# Driver Code
arr = [15, 10, 3, 2]
n = len(arr)
print(computeMaxValue(arr, n))
 
# This code is contributed by ninja_hattori.

                    

C#

using System;
 
class GFG
{
 
  // Function to find the index of minimum number of array
  static int minNum(float []a, int n)
  {
    int indOfMin = 0; // initializing with 0
 
    for (int i = 0; i < n; i++) {
      if (a[i] < a[indOfMin])
        indOfMin = i;
    }
    return indOfMin;
  }
 
  // Function to find the maximum pair
  // possible for the array
  static float computeMaxValue(float []a, int n)
  {
    int minIndex = minNum(a, n);
 
    float ans = Int32.MinValue;
 
    for (int i = 0; i < n; i++) {
      if (i != minIndex) {
        float temp
          = (float)a[i] / (float)a[minIndex];
        if (temp > ans)
          ans = temp;
      }
    }
 
    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 Kritima Gupta

                    

Javascript

<script>
 
// Javascript implementation to find
// the maximum pair in the array
 
// Function to find the index of minimum number of array
 
function minNum(a, n)
{
    var indOfMin = 0; // initializing with 0
 
    for (var i = 0; i < n; i++) {
        if (a[i] < a[indOfMin])
            indOfMin = i;
    }
    return indOfMin;
}
 
// Function to find the maximum pair
// possible for the array
function computeMaxValue( a,  n)
{
    var minIndex = minNum(a, n);
 
    var ans = Number.MIN_VALUE;
 
    for (var i = 0; i < n; i++) {
        if (i != minIndex) {
            var temp = Number(a[i]) / Number(a[minIndex]);
            ans = Math.max(ans, temp);
        }
    }
 
    return ans;
}
 
// Driver Code
 
var arr = [15, 10, 3, 2 ];
var n = arr.length;
document.write(computeMaxValue(arr, n));
 
// This code is contributed by ninja_hattori.
</script>

                    

Output
7.5

Time Complexity: O(N)

Auxiliary Space: O(1)



Last Updated : 30 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads