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++
#include <bits/stdc++.h>
using namespace std;
int maxMinSum(vector< int > arr, int K)
{
int N = arr.size();
multiset< int > s;
int sum = 0;
for ( int i = 0; i < K; i++) {
s.insert(arr[i]);
sum += arr[i];
}
int ans = sum * (*s.begin());
for ( int i = K; i < N; i++) {
sum += (arr[i] - arr[i - K]);
s.erase(s.find(arr[i - K]));
s.insert(arr[i]);
ans = max(ans, sum * (*s.begin()));
}
return ans;
}
int main()
{
vector< int > arr = { 3, 1, 5, 6, 4, 2 };
int K = 2;
cout << maxMinSum(arr, K);
return 0;
}
|
Java
import java.util.HashSet;
class GFG {
public static int maxMinSum( int [] arr, int K)
{
int N = arr.length;
HashSet<Integer> s = new HashSet<Integer>();
int sum = 0 ;
for ( int i = 0 ; i < K; i++) {
s.add(arr[i]);
sum += arr[i];
}
int ans = sum * (s.iterator().next());
for ( int i = K; i < N; i++) {
sum += (arr[i] - arr[i - K]);
if (s.contains(arr[i - K]))
s.remove(arr[i - K]);
s.add(arr[i]);
ans = Math.max(ans, sum * (s.iterator().next()));
}
return ans;
}
public static void main(String args[]) {
int [] arr = { 3 , 1 , 5 , 6 , 4 , 2 };
int K = 2 ;
System.out.println(maxMinSum(arr, K));
}
}
|
Python3
def maxMinSum(arr, K):
N = len (arr)
s = set ()
sum = 0
for i in range ( 0 , K):
s.add(arr[i])
sum + = arr[i]
ans = sum * ( list (s)[ 0 ])
for i in range (K, N):
sum + = (arr[i] - arr[i - K])
if arr[i - K] in s:
s.remove(arr[i - K])
s.add(arr[i])
ans = max (ans, sum * ( list (s)[ 0 ]))
return ans
if __name__ = = "__main__" :
arr = [ 3 , 1 , 5 , 6 , 4 , 2 ]
K = 2
print (maxMinSum(arr, K))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG {
public static int maxMinSum( int [] arr, int K)
{
int N = arr.Length;
HashSet< int > s = new HashSet< int >();
int sum = 0;
for ( int i = 0; i < K; i++) {
s.Add(arr[i]);
sum += arr[i];
}
int ans = sum * (s.ToList< int >()[0]);
for ( int i = K; i < N; i++) {
sum += (arr[i] - arr[i - K]);
if (s.Contains(arr[i - K]))
s.Remove(arr[i - K]);
s.Add(arr[i]);
ans = Math.Max(ans, sum * (s.ToList< int >()[0]));
}
return ans;
}
public static void Main() {
int [] arr = { 3, 1, 5, 6, 4, 2 };
int K = 2;
Console.Write(maxMinSum(arr, K));
}
}
|
Javascript
<script>
function MultiSet() {
let tm = {};
return { add, erase, first }
function add(x) {
tm[x] ? tm[x]++ : tm[x] = 1;
}
function erase(x) {
delete tm[x];
}
function first() {
let a = Object.keys(tm);
return a[0] - '0' ;
}
}
function maxMinSum(arr, K) {
let N = arr.length;
let s = new MultiSet();
let sum = 0;
for (let i = 0; i < K; i++) {
s.add(arr[i]);
sum += arr[i];
}
let ans = sum * (s.first());
for (let i = K; i < N; i++) {
sum += (arr[i] - arr[i - K]);
s.erase(arr[i - K]);
s.add(arr[i]);
ans = Math.max(ans, sum * (s.first()));
}
return ans;
}
let arr = [3, 1, 5, 6, 4, 2];
let K = 2;
document.write(maxMinSum(arr, K));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...