 GeeksforGeeks App
Open App Browser
Continue

# Maximize product of min value of subarray and sum of subarray over all subarrays of length K

Given an array arr[] of N integers, the task is to find the maximum possible value of (min * sum) among all possible subarrays having K elements, where min denotes the smallest integer of the subarray and sum denotes the sum of all elements of the subarray.

Example

Input: arr[] = {1, 2, 3, 2}, K = 3
Output: 14
Explanation: For the subarray {2, 3, 2}, the score is given as min(2, 3, 2) * sum(2, 3, 2) = 2 * 7 = 14, which is the maximum possible.

Input: arr[] = {3, 1, 5, 6, 4, 2}, K = 2
Output: 55

Approach: The above problem can be solved with the help of the sliding window technique by maintaining a window of K elements during the traversal of the array and keeping track of the minimum element and the sum of all elements in the current window in variables minimum and sum respectively. The minimum of all the K-sized subarrays can be calculated using a multiset data structure similar to the algorithm discussed here and the sum can be calculated using the algorithm discussed here. The maximum value of minimum * sum over all K-sized windows is the required answer.

Below is the implementation of the above approach:

## C++

 `// C++ implementation for the above approach``#include ``using` `namespace` `std;` `// Function to the maximum value of min * sum``// over all possible subarrays of K elements``int` `maxMinSum(vector<``int``> arr, ``int` `K)``{``    ``// Store the array size``    ``int` `N = arr.size();` `    ``// Multiset data structure to calculate the``    ``// minimum over all K sized subarrays``    ``multiset<``int``> s;` `    ``// Stores the sum of the current window``    ``int` `sum = 0;` `    ``// Loop to calculate the sum and min of the``    ``// 1st window of size K``    ``for` `(``int` `i = 0; i < K; i++) {``        ``s.insert(arr[i]);``        ``sum += arr[i];``    ``}` `    ``// Stores the required answer``    ``int` `ans = sum * (*s.begin());` `    ``// Loop to iterate over the remaining windows``    ``for` `(``int` `i = K; i < N; i++) {` `        ``// Add the current value and remove the``        ``// (i-K)th value from the sum``        ``sum += (arr[i] - arr[i - K]);` `        ``// Update the set``        ``s.erase(s.find(arr[i - K]));``        ``s.insert(arr[i]);` `        ``// Update answer``        ``ans = max(ans, sum * (*s.begin()));``    ``}` `    ``// Return Answer``    ``return` `ans;``}` `// Driver Code``int` `main()``{``    ``vector<``int``> arr = { 3, 1, 5, 6, 4, 2 };``    ``int` `K = 2;` `    ``cout << maxMinSum(arr, K);` `    ``return` `0;``}`

## Java

 `// Java implementation for the above approach``import` `java.util.HashSet;` `class` `GFG {` `    ``// Function to the maximum value of min * sum``    ``// over all possible subarrays of K elements``    ``public` `static` `int` `maxMinSum(``int``[] arr, ``int` `K)``    ``{``      ` `        ``// Store the array size``        ``int` `N = arr.length;` `        ``// Multiset data structure to calculate the``        ``// minimum over all K sized subarrays``        ``HashSet s = ``new` `HashSet();` `        ``// Stores the sum of the current window``        ``int` `sum = ``0``;` `        ``// Loop to calculate the sum and min of the``        ``// 1st window of size K``        ``for` `(``int` `i = ``0``; i < K; i++) {``            ``s.add(arr[i]);``            ``sum += arr[i];``        ``}` `        ``// Stores the required answer``        ``int` `ans = sum * (s.iterator().next());` `        ``// Loop to iterate over the remaining windows``        ``for` `(``int` `i = K; i < N; i++) {` `            ``// Add the current value and remove the``            ``// (i-K)th value from the sum``            ``sum += (arr[i] - arr[i - K]);` `            ``// Update the set``            ``if` `(s.contains(arr[i - K]))``                ``s.remove(arr[i - K]);``            ``s.add(arr[i]);` `            ``// Update answer``            ``ans = Math.max(ans, sum * (s.iterator().next()));``        ``}` `        ``// Return Answer``        ``return` `ans;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String args[]) {``        ``int``[] arr = { ``3``, ``1``, ``5``, ``6``, ``4``, ``2` `};``        ``int` `K = ``2``;` `        ``System.out.println(maxMinSum(arr, K));``    ``}``}` `// This code is contributed by saurabh_jaiswal.`

## Python3

 `# python implementation for the above approach` `# Function to the maximum value of min * sum``# over all possible subarrays of K elements``def` `maxMinSum(arr, K):` `    ``# Store the array size``    ``N ``=` `len``(arr)` `    ``# Multiset data structure to calculate the``    ``# minimum over all K sized subarrays``    ``s ``=` `set``()` `    ``# Stores the sum of the current window``    ``sum` `=` `0` `    ``# Loop to calculate the sum and min of the``    ``# 1st window of size K``    ``for` `i ``in` `range``(``0``, K):``        ``s.add(arr[i])``        ``sum` `+``=` `arr[i]` `    ``# Stores the required answer``    ``ans ``=` `sum` `*` `(``list``(s)[``0``])` `    ``# Loop to iterate over the remaining windows``    ``for` `i ``in` `range``(K, N):` `       ``# Add the current value and remove the``       ``# (i-K)th value from the sum``        ``sum` `+``=` `(arr[i] ``-` `arr[i ``-` `K])` `        ``# Update the set``        ``if` `arr[i``-``K] ``in` `s:``            ``s.remove(arr[i``-``K])` `        ``s.add(arr[i])` `        ``# Update answer``        ``ans ``=` `max``(ans, ``sum` `*` `(``list``(s)[``0``]))` `        ``# Return Answer``    ``return` `ans` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``arr ``=` `[``3``, ``1``, ``5``, ``6``, ``4``, ``2``]``    ``K ``=` `2` `    ``print``(maxMinSum(arr, K))` `    ``# This code is contributed by rakeshsahni`

## C#

 `// C# implementation for the above approach``using` `System;``using` `System.Collections.Generic;``using` `System.Linq;` `class` `GFG {` `    ``// Function to the maximum value of min * sum``    ``// over all possible subarrays of K elements``    ``public` `static` `int` `maxMinSum(``int``[] arr, ``int` `K)``    ``{``      ` `        ``// Store the array size``        ``int` `N = arr.Length;` `        ``// Multiset data structure to calculate the``        ``// minimum over all K sized subarrays``        ``HashSet<``int``> s = ``new` `HashSet<``int``>();` `        ``// Stores the sum of the current window``        ``int` `sum = 0;` `        ``// Loop to calculate the sum and min of the``        ``// 1st window of size K``        ``for` `(``int` `i = 0; i < K; i++) {``            ``s.Add(arr[i]);``            ``sum += arr[i];``        ``}` `        ``// Stores the required answer``        ``int` `ans = sum * (s.ToList<``int``>());`  `        ``// Loop to iterate over the remaining windows``        ``for` `(``int` `i = K; i < N; i++) {` `            ``// Add the current value and remove the``            ``// (i-K)th value from the sum``            ``sum += (arr[i] - arr[i - K]);` `            ``// Update the set``            ``if` `(s.Contains(arr[i - K]))``                ``s.Remove(arr[i - K]);``            ``s.Add(arr[i]);` `            ``// Update answer``            ``ans = Math.Max(ans, sum * (s.ToList<``int``>()));``        ``}` `        ``// Return Answer``        ``return` `ans;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main() {``        ``int``[] arr = { 3, 1, 5, 6, 4, 2 };``        ``int` `K = 2;` `        ``Console.Write(maxMinSum(arr, K));``    ``}``}` `// This code is contributed by saurabh_jaiswal.`

## Javascript

 ``

Output:

`55`

Time Complexity: O(N*log N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up