Given an array arr[] and an integer K, the task is to calculate the ratio of all subarrays of size K.
Examples:
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.5Input: arr[] = {1, -2, 3, -4, 5, 6}, K = 2
Output: -0.5 -0.666667 -0.75 -0.8 0.833333
Approach: The idea is to iterate over every subarray of size K present in the given array and follow the steps below to solve the problem:
- Initialize a variable, say ratio with the first element of the subarray.
- Iterate over the remaining subarray and keep on dividing ratio by the encountered elements one by one.
- Finally, print the final value of ratio for that subarray.
- Repeat the above steps for all subarrays.
Below is the implementation of the above approach:
// C++ implementation of the above approach #include <bits/stdc++.h> 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 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 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# 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 |
<script> // Javascript implementation of the above approach // Function to find the ratio of // all subarrays of size K function calcRatio(arr, n, k)
{ // Traverse every subarray of size K
for ( var i = 0; i <= n - k; i++) {
// Initialize ratio
var ratio = arr[i];
// Calculate ratio of the
// current subarray
for ( var j = i + 1; j < k + i; j++)
ratio /= arr[j];
// Print ratio of the subarray
document.write(ratio + " " );
}
} // Driver Code // Given array var arr = [ 24, 3, 2, 1 ];
var n = arr.length;
var k = 3;
// Function Call calcRatio(arr, n, k); </script> |
4 1.5
Time Complexity: O(N2)
Auxiliary Space: O(1)