# Largest product of a subarray of size k

Given an array consisting of n positive integers, and an integer k. Find the largest product subarray of size k, i.e., find maximum produce of k contiguous elements in the array where k <= n.

Examples :

```Input: arr[] = {1, 5, 9, 8, 2, 4,
1, 8, 1, 2}
k = 6
Output:   4608
The subarray is {9, 8, 2, 4, 1, 8}

Input: arr[] = {1, 5, 9, 8, 2, 4, 1, 8, 1, 2}
k = 4
Output:   720
The subarray is {5, 9, 8, 2}

Input: arr[] = {2, 5, 8, 1, 1, 3};
k = 3
Output:   80
The subarray is {2, 5, 8}
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Method 1 (Simple : O(n*k))
A Naive approach would be to consider all the subarrays of size k one by one. Such a approach would require two loops hence the complexity would be O(n*k).

Method 2 (Efficient : O(n))
We can solve it in O(n) by using the fact that product of a subarray of size k can be computed in O(1) time if we have product of previous subarray available with us.

```curr_product = (prev_product / arr[i-1]) * arr[i + k -1]

prev_product : Product of subarray of size k beginning
with arr[i-1]

curr_product : Product of subarray of size k beginning
with arr[i]
```

In this way we can compute the maximum k size subarray product in only one traversal. Below is C++ implementation of the idea.

## C++

 `// C++ program to find the maximum product of a subarray ` `// of size k. ` `#include ` `using` `namespace` `std; ` ` `  `// This function returns maximum product of a subarray ` `// of size k in given arrar, arr[0..n-1]. This function ` `// assumes that k is smaller than or equal to n. ` `int` `findMaxProduct(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``// Initialize the MaxProduct to 1, as all elements ` `    ``// in the array are positive ` `    ``int` `MaxProduct = 1; ` `    ``for` `(``int` `i=0; i

## Java

 `// Java program to find the maximum product of a subarray ` `// of size k ` `import` `java.io.*; ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` `    ``// Function returns maximum product of a subarray ` `    ``// of size k in given arrar, arr[0..n-1]. This function ` `    ``// assumes that k is smaller than or equal to n. ` `    ``static` `int` `findMaxProduct(``int` `arr[], ``int` `n, ``int` `k) ` `    ``{ ` `        ``// Initialize the MaxProduct to 1, as all elements ` `        ``// in the array are positive ` `        ``int` `MaxProduct = ``1``; ` `        ``for` `(``int` `i=``0``; i

## Python3

 `# Python 3 program to find the maximum  ` `# product of a subarray of size k. ` ` `  `# This function returns maximum product  ` `# of a subarray of size k in given arrar, ` `# arr[0..n-1]. This function assumes  ` `# that k is smaller than or equal to n. ` `def` `findMaxProduct(arr, n, k) : ` `   `  `    ``# Initialize the MaxProduct to 1,  ` `    ``# as all elements in the array  ` `    ``# are positive ` `    ``MaxProduct ``=` `1` `    ``for` `i ``in` `range``(``0``, k) : ` `        ``MaxProduct ``=` `MaxProduct ``*` `arr[i] ` `         `  `    ``prev_product ``=` `MaxProduct ` `  `  `    ``# Consider every product beginning ` `    ``# with arr[i] where i varies from ` `    ``# 1 to n-k-1 ` `    ``for` `i ``in` `range``(``1``, n ``-` `k ``+` `1``) : ` `        ``curr_product ``=` `(prev_product ``/``/` `arr[i``-``1``]) ``*` `arr[i``+``k``-``1``] ` `        ``MaxProduct ``=` `max``(MaxProduct, curr_product) ` `        ``prev_product ``=` `curr_product ` `     `  `     `  `    ``# Return the maximum product found ` `    ``return` `MaxProduct ` `     `  `# Driver code ` `arr1 ``=` `[``1``, ``5``, ``9``, ``8``, ``2``, ``4``, ``1``, ``8``, ``1``, ``2``] ` `k ``=` `6` `n ``=` `len``(arr1) ` `print` `(findMaxProduct(arr1, n, k) ) ` `k ``=` `4` `print` `(findMaxProduct(arr1, n, k)) ` ` `  `arr2 ``=` `[``2``, ``5``, ``8``, ``1``, ``1``, ``3``] ` `k ``=` `3` `n ``=` `len``(arr2) ` ` `  `print``(findMaxProduct(arr2, n, k)) ` ` `  `# This code is contributed by Nikita Tiwari. `

## C#

 `// C# program to find the maximum  ` `// product of a subarray of size k ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `    ``// Function returns maximum  ` `    ``// product of a subarray of  ` `    ``// size k in given arrar,  ` `    ``// arr[0..n-1]. This function  ` `    ``// assumes that k is smaller  ` `    ``// than or equal to n. ` `    ``static` `int` `findMaxProduct(``int` `[]arr,  ` `                              ``int` `n, ``int` `k) ` `    ``{ ` `        ``// Initialize the MaxProduct  ` `        ``// to 1, as all elements ` `        ``// in the array are positive ` `        ``int` `MaxProduct = 1; ` `        ``for` `(``int` `i = 0; i < k; i++) ` `            ``MaxProduct *= arr[i]; ` ` `  `        ``int` `prev_product = MaxProduct; ` ` `  `        ``// Consider every product beginning  ` `        ``// with arr[i] where i varies from  ` `        ``// 1 to n-k-1 ` `        ``for` `(``int` `i = 1; i <= n - k; i++) ` `        ``{ ` `            ``int` `curr_product = (prev_product /  ` `                                 ``arr[i - 1]) *  ` `                                 ``arr[i + k - 1]; ` `            ``MaxProduct = Math.Max(MaxProduct,  ` `                                  ``curr_product); ` `            ``prev_product = curr_product; ` `        ``} ` ` `  `        ``// Return the maximum ` `        ``// product found ` `        ``return` `MaxProduct; ` `    ``} ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `Main ()  ` `    ``{ ` `        ``int` `[]arr1 = {1, 5, 9, 8, 2,  ` `                      ``4, 1, 8, 1, 2}; ` `        ``int` `k = 6; ` `        ``int` `n = arr1.Length; ` `        ``Console.WriteLine(findMaxProduct(arr1, n, k)); ` ` `  `        ``k = 4; ` `        ``Console.WriteLine(findMaxProduct(arr1, n, k)); ` ` `  `        ``int` `[]arr2 = {2, 5, 8, 1, 1, 3}; ` `        ``k = 3; ` `        ``n = arr2.Length; ` `        ``Console.WriteLine(findMaxProduct(arr2, n, k)); ` `    ``} ` `} ` ` `  `// This code is contributed by anuj_67. `

## PHP

 ` `

Output :

```4608
720
80
```

This article is contributed by Ashutosh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Improved By : vt_m

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.