Check if the product of the K largest sums of subarrays is greater than M
Given an array arr[] of N integers and two integers M and K. The task is to check if the product of the K largest sum of contiguous subarrays is greater than M.
Examples:
Input: arr[] = {10, -4, -2, 7}, M = 659, K = 3
Output: Yes The 3 largest contiguous sums for the subarrays are of the subarrays {10, -4, -2, 7}, {10} and {7} i.e. 11, 10 and 7, the product is 11 * 10 * 7 = 770 which is greater than 659.
Input: arr[] = {4, -3, 8}, M = 100, K = 6
Output: No
A brute force approach is to store all the sum of the contiguous subarray in some other array and sort it then calculate the product of the K largest sum and check if the value is greater than M. But in case of the size of the array being too large, the number of continuous subarrays will increase, and hence the auxiliary array will take more space. A better approach is to store the prefix sum of the array in the array itself. Then the sum of the subarray arr[i…j] can be calculated as arr[j] – arr[i – 1]. Now for storing the K largest sum contiguous subarrays, use a min-heap (priority queue) in which only the K maximum sums will be stored at a time. After that for every other element, check if the element is greater than the top element of the min-heap if yes then that element will be inserted into the min-heap and the top element will be popped from the min-heap. In the end, calculate the product of all the elements in the min-heap and check if it is greater than M or not.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
bool checkKSum( int arr[], int n, int k, int M)
{
for ( int i = 1; i < n; i++)
arr[i] = arr[i - 1] + arr[i];
priority_queue< int , vector< int >, greater< int > > Q;
for ( int i = 0; i < n; i++) {
for ( int j = i; j < n; j++) {
int sum
= (i == 0) ? arr[j] : arr[j] - arr[i - 1];
if (Q.size() < k)
Q.push(sum);
else {
if (Q.top() < sum) {
Q.pop();
Q.push(sum);
}
}
}
}
long product = 1;
while (!Q.empty()) {
product *= Q.top();
Q.pop();
}
if (product > M)
return true ;
return false ;
}
int main()
{
int a[] = { 10, -4, -2, 7 };
int n = sizeof (a) / sizeof (a[0]);
int k = 3, M = 659;
if (checkKSum(a, n, k, M))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static boolean checkKSum( int arr[], int n, int k,
int M)
{
for ( int i = 1 ; i < n; i++)
arr[i] = arr[i - 1 ] + arr[i];
PriorityQueue<Integer> Q
= new PriorityQueue<Integer>();
for ( int i = 0 ; i < n; i++) {
for ( int j = i; j < n; j++) {
int sum = (i == 0 ) ? arr[j]
: arr[j] - arr[i - 1 ];
if (Q.size() < k)
Q.add(sum);
else {
if (Q.poll() < sum) {
Q.add(sum);
}
}
}
}
long product = 1 ;
while (Q.isEmpty() == false ) {
product = product * Q.poll();
}
if (product > M)
return true ;
return false ;
}
public static void main(String[] args)
{
int a[] = { 10 , - 4 , - 2 , 7 };
int n = a.length;
int k = 3 , M = 659 ;
if (checkKSum(a, n, k, M))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
import heapq
def checkKSum(arr, n, k, M):
for i in range ( 1 , n):
arr[i] + = arr[i - 1 ]
Q = arr
heapq.heapify(Q)
for i in range (n):
for j in range (i, n):
if i = = 0 :
sum = 0
else :
sum = arr[j] - arr[i - 1 ]
if len (Q) < k:
Q.append( sum )
heapq.heapify(Q)
else :
if Q[ 0 ] < sum :
Q[ 0 ] = sum
heapq.heapify(Q)
product = 1
for i in Q:
product * = i
if product > M:
return True
return False
if __name__ = = '__main__' :
a = [ 10 , - 4 , - 2 , 7 ]
n = len (a)
k, M = 3 , 659
if checkKSum(a, n, k, M):
print ( 'Yes' )
else :
print ( 'No' )
|
C#
using System;
using System.Collections;
public class GFG
{
public static bool checkKSum( int [] arr, int n, int k,
int M)
{
for ( int i = 1; i < n; i++)
arr[i] = arr[i - 1] + arr[i];
SortedList Q = new SortedList();
for ( int i = 0; i < n; i++) {
for ( int j = i; j < n; j++) {
int sum = (i == 0) ? arr[j]
: arr[j] - arr[i - 1];
if (Q.Count < k)
Q.Add(sum, sum);
else {
int elem = ( int )Q.GetByIndex(0);
Q.RemoveAt(0);
if (elem < sum) {
Q.Add(sum, sum);
}
}
}
}
long product = 1;
while (Q.Count != 0) {
int elem = ( int )Q.GetByIndex(0);
Q.RemoveAt(0);
product = product * elem;
}
if (product > M)
return true ;
return false ;
}
public static void Main( string [] args)
{
int [] a = { 10, -4, -2, 7 };
int n = a.Length;
int k = 3, M = 659;
if (checkKSum(a, n, k, M))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
function checkKSum(arr, n, k, M)
{
var elem;
var sum;
for ( var i = 1; i < n; i++)
arr[i] += arr[i - 1];
let Q = arr;
for ( var i = 0; i < n; i++) {
for ( var j = i; j < n; j++) {
if (i == 0)
sum = 0;
else
sum = arr[j] - arr[i - 1];
Q.sort();
if (Q.length < k)
{
Q.push(sum);
}
else {
if (Q[0] < sum) {
Q[0] = sum;
}
Q.sort();
}
}
}
var product = 1;
for ( var elem of Q)
product *= elem;
if (product > M)
return true ;
return false ;
}
let a = [ 10, -4, -2, 7 ];
let n = a.length;
let k = 3;
let M = 659;
if (checkKSum(a, n, k, M))
console.log( "Yes" );
else
console.log( "No" );
|
Time Complexity: O(n2log n)
Auxiliary Space: O(n)
Last Updated :
14 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...