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++
// 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; } |
Java
// 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 |
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 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 |
C#
// 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 |
Output :
3
Time complexity : O(N)
Auxiliary space : O(1)
Note: Above solution works only for positive integers
Recommended Posts:
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Maximum XOR of Two Numbers in an Array
- Number of digits before the decimal point in the division of two numbers
- Maximum no. of contiguous Prime Numbers in an array
- Maximum length of a sub-array with ugly numbers
- Maximum consecutive numbers present in an array
- Minimum and Maximum prime numbers in an array
- Find all numbers that divide maximum array elements
- Maximum count of equal numbers in an array after performing given operations
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Maximum subarray sum in array formed by repeating the given array k times
- Rearrange numbers in an array such that no two adjacent numbers are same
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.



