给定一个数组arr[]和一个整数K ,任务是计算大小为K的所有子数组的比率。
例子:
Input: arr[] = {24, 3, 2, 1}, K = 3
Output: 4 1.5
Explanation:
All subarrays of size K and their ratio:
Subarray 1: {24, 3, 2} = 24 / 3 / 2 = 4
Subarray 2: {3, 2, 1} = 3 / 2 / 1= 1.5
Input: arr[] = {1, -2, 3, -4, 5, 6}, K = 2
Output: -0.5 -0.666667 -0.75 -0.8 0.833333
方法:思想是迭代给定数组中存在的每个大小为 K 的子数组,并按照以下步骤解决问题:
- 用子数组的第一个元素初始化一个变量,比如ratio 。
- 迭代剩余的子数组并继续将比率除以遇到的元素一一。
- 最后,打印该子数组的比率的最终值。
- 对所有子数组重复上述步骤。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to find the ratio of
// all subarrays of size K
int calcRatio(double arr[], int n, int k)
{
// Traverse every subarray of size K
for (int i = 0; i <= n - k; i++) {
// Initialize ratio
double ratio = arr[i];
// Calculate ratio of the
// current subarray
for (int j = i + 1; j < k + i; j++)
ratio /= arr[j];
// Print ratio of the subarray
cout << ratio << " ";
}
}
// Driver Code
int main()
{
// Given array
double arr[] = { 24, 3, 2, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
int k = 3;
// Function Call
calcRatio(arr, n, k);
return 0;
}
Java
// Java implementation of the above approach
import java.util.*;
class GFG{
// Function to find the ratio of
// all subarrays of size K
static void calcRatio(double arr[], int n,
int k)
{
// Traverse every subarray of size K
for(int i = 0; i <= n - k; i++)
{
// Initialize ratio
double ratio = arr[i];
// Calculate ratio of the
// current subarray
for(int j = i + 1; j < k + i; j++)
ratio /= arr[j];
// Print ratio of the subarray
System.out.print(ratio + " ");
}
}
// Driver code
public static void main (String[] args)
{
// Given array
double arr[] = { 24, 3, 2, 1 };
int n = arr.length;
int k = 3;
// Function call
calcRatio(arr, n, k);
}
}
// This code is contributed by offbeat
Python3
# Python3 implementation
# of the above approach
# Function to find the ratio of
# all subarrays of size K
def calcRatio(arr, n, k):
# Traverse every subarray
# of size K
for i in range(n - k + 1):
# Initialize ratio
ratio = arr[i]
# Calculate ratio of the
# current subarray
for j in range(i + 1, k + i):
ratio = ratio / arr[j]
# Print ratio of the subarray
print(ratio, end = " ")
# Given array
arr = [24, 3, 2, 1]
n = len(arr)
k = 3
# Function Call
calcRatio(arr, n, k)
# This code is contributed by divyeshrabadiya07
C#
// C# implementation of the above approach
using System;
class GFG{
// Function to find the ratio of
// all subarrays of size K
static void calcRatio(double []arr, int n,
int k)
{
// Traverse every subarray of size K
for(int i = 0; i <= n - k; i++)
{
// Initialize ratio
double ratio = arr[i];
// Calculate ratio of the
// current subarray
for(int j = i + 1; j < k + i; j++)
ratio /= arr[j];
// Print ratio of the subarray
Console.Write(ratio + " ");
}
}
// Driver code
public static void Main(string[] args)
{
// Given array
double []arr = { 24, 3, 2, 1 };
int n = arr.Length;
int k = 3;
// Function call
calcRatio(arr, n, k);
}
}
// This code is contributed by rutvik_56
Javascript
输出
4 1.5
时间复杂度: O(N 2 )
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live