📜  最大比率连续子数组

📅  最后修改于: 2021-05-06 20:38:11             🧑  作者: Mango

给定N个数字的数组arr [] ,任务是从给定数组中找到最大比例的连续子数组。

例子:

方法:想法是生成数组的所有子数组,并为每个子数组找到子数组的比率为arr [i] / arr [i + 1] / arr [i + 2],依此类推。跟踪最大比率,并在最后返回。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to return maximum
// of two double values
double maximum(double a, double b)
{
    // Check if a is greater
    // than b then return a
    if (a > b)
        return a;
  
    return b;
}
  
// Function that returns the
// Ratio of max Ratio subarray
double maxSubarrayRatio(
  double arr[], int n)
{
    
    // Variable to store
    // the maximum ratio
    double maxRatio = INT_MIN;
  
    // Compute the product while
    // traversing for subarrays
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            
            double ratio = arr[i];
            
            for (int k = i + 1; k <= j; k++) {
                
                // Calculate the ratio
                ratio = ratio / arr[k];
            }
            
            // Update max ratio
            maxRatio = maximum(maxRatio, ratio);
        }
    }
  
    // Print the answer
    return maxRatio;
}
  
// Driver code
int main()
{
    double arr[] = { 2, 2, 4, -0.2, -1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << maxSubarrayRatio(arr, n);
    return 0;
}


Java
// Java program for the above approach
class GFG{
      
// Function to return maximum
// of two double values
static double maximum(double a, double b)
{
      
    // Check if a is greater
    // than b then return a
    if (a > b)
        return a;
  
    return b;
}
  
// Function that returns the
// Ratio of max Ratio subarray
static double maxSubarrayRatio(double arr[],
                               int n)
{
      
    // Variable to store
    // the maximum ratio
    double maxRatio = Integer.MIN_VALUE;
  
    // Compute the product while
    // traversing for subarrays
    for(int i = 0; i < n; i++) 
    {
        for(int j = i; j < n; j++) 
        {
            double ratio = arr[i];
              
            for(int k = i + 1; k <= j; k++)
            {
                  
                // Calculate the ratio
                ratio = ratio / arr[k];
            }
              
            // Update max ratio
            maxRatio = maximum(maxRatio, ratio);
        }
    }
  
    // Print the answer
    return maxRatio;
}
      
// Driver code    
public static void main(String[] args) 
{
    double arr[] = { 2, 2, 4, -0.2, -1 };
    int n = arr.length;
      
    System.out.println(maxSubarrayRatio(arr, n));
}
}
  
// This code is contributed by rutvik_56


Python3
# Python3 program for the above approach
import sys
  
# Function to return maximum
# of two double values
def maximum(a, b):
  
    # Check if a is greater
    # than b then return a
    if (a > b):
        return a
  
    return b
  
# Function that returns the
# Ratio of max Ratio subarray
def maxSubarrayRatio(arr, n):
  
    # Variable to store
    # the maximum ratio
    maxRatio = -sys.maxsize - 1
  
    # Compute the product while
    # traversing for subarrays
    for i in range(n):
        for j in range(i, n):
            ratio = arr[i]
          
            for k in range(i + 1, j + 1):
              
                # Calculate the ratio
                ratio = ratio // arr[k]
          
            # Update max ratio
            maxRatio = maximum(maxRatio, ratio)
          
    # Print the answer
    return int(maxRatio)
  
# Driver code
if __name__ == "__main__":
      
    arr = [ 2, 2, 4, -0.2, -1 ]
    n = len(arr)
      
    print(maxSubarrayRatio(arr, n))
  
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
  
class GFG{
      
// Function to return maximum
// of two double values
static double maximum(double a, double b)
{
      
    // Check if a is greater
    // than b then return a
    if (a > b)
        return a;
  
    return b;
}
  
// Function that returns the
// Ratio of max Ratio subarray
static double maxSubarrayRatio(double []arr,
                               int n)
{
      
    // Variable to store
    // the maximum ratio
    double maxRatio = int.MinValue;
  
    // Compute the product while
    // traversing for subarrays
    for(int i = 0; i < n; i++) 
    {
        for(int j = i; j < n; j++) 
        {
            double ratio = arr[i];
              
            for(int k = i + 1; k <= j; k++)
            {
                  
                // Calculate the ratio
                ratio = ratio / arr[k];
            }
              
            // Update max ratio
            maxRatio = maximum(maxRatio, ratio);
        }
    }
  
    // Print the answer
    return maxRatio;
}
      
// Driver code 
public static void Main(String[] args) 
{
    double []arr = { 2, 2, 4, -0.2, -1 };
    int n = arr.Length;
      
    Console.WriteLine(maxSubarrayRatio(arr, n));
}
}
  
// This code is contributed by 29AjayKumar


输出
20

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