📜  数组中两个数字相除的最大值

📅  最后修改于: 2021-05-06 08:32:55             🧑  作者: Mango

给定大小为N(> 2)的数组A。任务是找到A [i] / A [j]的最大值

注意: A [i]≠0。

例子:

天真的方法:天真的方法是运行嵌套循环并找到最大可能的答案。

时间复杂度:O(N 2 )。

高效的方法:一种有效的方法是在数组中找到最大最小元素并打印最大/最小

证明:

下面是上述方法的实现:

C++
// CPP program to maximum value of 
// division of two numbers in an array
#include 
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 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 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# 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 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


输出 :

3

时间复杂度: O(N)
辅助空间:O(1)

注意:以上解决方案仅适用于正整数