Skip to content
Related Articles

Related Articles

Improve Article

Ratio of all subarrays of size K

  • Last Updated : 17 Mar, 2021

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.5

Input: 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: 



  1. Initialize a variable, say ratio with the first element of the subarray.
  2. Iterate over the remaining subarray and keep on dividing ratio by the encountered elements one by one.
  3. Finally, print the final value of ratio for that subarray.
  4. 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 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




<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>
Output
4 1.5 

Time Complexity: O(N2
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Participate in the Scholorship Test for First-Step-to-DSA Course for Class 9 to 12 students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :