给定N个数字的数组arr [] ,任务是从给定数组中找到最大比例的连续子数组。
例子:
Input: arr = { -1, 10, 0.1, -8, -2 }
Output: 100
Explanation:
The subarray {10, 0.1} gives 10 / 0.1 = 100 which is the largest ratio.
Input: arr = { 2, 2, 4, -0.2, -1 }
Output: 20
Explanation:
The subarray {4, -0.2, -1} has the largest ratio as 20.
方法:想法是生成数组的所有子数组,并为每个子数组找到子数组的比率为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
Javascript
输出
20
时间复杂度: (N 3 )
辅助空间: O(1)