Maximum value of division of two numbers in an Array

Given an array A of size N (> 2). The task is to find the maximum value of A[i] / A[j]

Note: A[i] ≠ 0.

Examples:



Input : A[] = {1, 2, 3, 4}
Output : 4
4 / 1 = 4 is maximum possible value.

Input : A[] = {3, 7, 9, 3, 11}
Output : 3

Naive Approach: A naive approach is to run nested loops and find the maximum possible answer.

Time complexity : O(N2) .

Efficient Approach: An efficient approach is to find maximum and minimum element in the array and print maximum / minimum

Proof:

Lets take an array A[] = {a, b, c, d} where a < b < c < d .
Now :
(b / a) < ( c / a ) < (d / a) (since b < c < d, and denominator is constant )
and since a is minimum, therefore
d / a is maximum

Below is the implemenantion of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to maximum value of 
// division of two numbers in an array
#include <bits/stdc++.h>
using namespace std;
  
// Function to maximum value of 
// division of two numbers in an array
int Divison(int a[], int n)
{
    int maxi = INT_MIN, mini = INT_MAX;
      
    // Traverse through the array
    for (int i = 0; i < n; i++)
    {
        maxi = max(a[i], maxi);
        mini = min(a[i], mini);
    }
      
    // Return the required answer
    return maxi / mini;
}
  
// Driver code
int main() 
{
    int a[] = {3, 7, 9, 3, 11};
      
    int n = sizeof(a) / sizeof(a[0]);
      
    cout << Divison(a, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java progarm to maximum value of 
// division of two numbers in an array
import java.util.*;
import java.lang.*;
import java.io.*;
  
class GFG
{
  
// Function to maximum value of 
// division of two numbers in an array
static int Divison(int a[], int n)
{
    int maxi = Integer.MIN_VALUE, 
        mini = Integer.MAX_VALUE;
      
    // Traverse through the array
    for (int i = 0; i < n; i++)
    {
        maxi = Math.max(a[i], maxi);
        mini = Math.min(a[i], mini);
    }
      
    // Return the required answer
    return maxi / mini;
}
  
// Driver code
public static void main (String[] args)
              throws java.lang.Exception
{
    int a[] = {3, 7, 9, 3, 11};
      
    int n = a.length;
      
    System.out.print(Divison(a, n));
}
}
  
// This code is contributed by Nidhiva

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

#  Python3 program to maximum value of
# division of two numbers in an array
  
# Function to maximum value of
# division of two numbers in an array
def Divison(a, n):
  
    maxi = -10**9
    mini = 10**9
  
    # Traverse through the array
    for i in a:
        maxi = max(i, maxi)
        mini = min(i, mini)
  
    # Return the required answer
    return maxi // mini
  
# Driver code
a = [3, 7, 9, 3, 11]
  
n = len(a)
  
print(Divison(a, n))
  
# This code is contributed by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# progarm to maximum value of 
// division of two numbers in an array
using System;
      
class GFG
{
  
// Function to maximum value of 
// division of two numbers in an array
static int Divison(int []a, int n)
{
    int maxi = int.MinValue, 
        mini = int.MaxValue;
      
    // Traverse through the array
    for (int i = 0; i < n; i++)
    {
        maxi = Math.Max(a[i], maxi);
        mini = Math.Min(a[i], mini);
    }
      
    // Return the required answer
    return maxi / mini;
}
  
// Driver code
public static void Main (String[] args)
{
    int []a = {3, 7, 9, 3, 11};
      
    int n = a.Length;
      
    Console.WriteLine(Divison(a, n));
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output :

3

Time complexity : O(N)
Auxiliary space : O(1)

Note: Above solution works only for positive integers



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.