Ratio of all subarrays of size K
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++
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std;// Function to find the ratio of// all subarrays of size Kint 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 Codeint 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 approachimport java.util.*;class GFG{ // Function to find the ratio of// all subarrays of size Kstatic 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 codepublic 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 Kdef 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 arrayarr = [24, 3, 2, 1]n = len(arr)k = 3# Function CallcalcRatio(arr, n, k)# This code is contributed by divyeshrabadiya07 |
C#
// C# implementation of the above approachusing System; class GFG{ // Function to find the ratio of// all subarrays of size Kstatic 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 codepublic 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
<script>// Javascript implementation of the above approach// Function to find the ratio of// all subarrays of size Kfunction 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 arrayvar arr = [ 24, 3, 2, 1 ];var n = arr.length;var k = 3;// Function CallcalcRatio(arr, n, k);</script> |
Output
4 1.5
Time Complexity: O(N2)
Auxiliary Space: O(1)


