Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Maximum value of division of two numbers in an Array

  • Last Updated : 21 May, 2021

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: 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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

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



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 implementation of the above approach:  

C++




// 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 Division(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 << Division(a, n);
    return 0;
}

Java




// Java program 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 Division(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(Division(a, n));
}
}
 
// This code is contributed by Nidhiva

Python3




#  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 Division(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(Division(a, n))
 
# This code is contributed by Mohit Kumar

C#




// C# program 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 Division(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(Division(a, n));
}
}
 
// This code is contributed by Rajput-Ji

Javascript




<script>
// Javascript program to maximum value of
// division of two numbers in an array
 
// Function to maximum value of
// division of two numbers in an array
function Division(a, n)
{
    let maxi = Number.MIN_VALUE, mini = Number.MAX_VALUE;
     
    // Traverse through the array
    for (let i = 0; i < n; i++)
    {
        maxi = Math.max(a[i], maxi);
        mini = Math.min(a[i], mini);
    }
     
    // Return the required answer
    return parseInt(maxi / mini);
}
 
// Driver code
    let a = [3, 7, 9, 3, 11];
     
    let n = a.length;
     
    document.write(Division(a, n));
 
</script>

Output : 

3

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

Note: Above solution works only for positive integers
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :