Codeforces Round 1133 (Div. 1, Div. 2) is coming soon! Register here. ×

Max Sum in a window

Revision en3, by excel8, 2022-03-19 20:36:10

Given an array, a window must be chosen such that this component (sum(a[i] + a[i+1] + a[i+2] ... a[j])* (j-i+1) is maximized where i and j are indices and i<j and the component value must be equal or less than the threshold . (sum(a[i] + a[i+1] + a[i+2] ... a[j])* (j-i+1) <= threshold).

How we can solve this question optimally? Aim is to find the closest value that is less than or equal to threshold.

One example of the problem:

Constraints:

N<=10^5 Threshold<=10^5 A[i]<=10^5

Input N: 8 Arr[]: [2,-3,-4,5,5,6,7,8] Threshold = 20

Output: One possible answer 20

Explanation of output Index:[3, 4] = (5+5)*2 = 20 <= 20 (threshold) and it is the maximum value we can get.

Tags sliding window

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English excel8 2022-03-19 20:37:05 24 Tiny change: 'N<=10^5 \nThreshold<=10^5\nA[i]<=10' -> 'N<=10^5 \n\nThreshold<=10^5\n\nA[i]<=10'
en3 English excel8 2022-03-19 20:36:10 60
en2 English excel8 2022-03-19 18:44:57 84
en1 English excel8 2022-03-19 18:03:05 590 Initial revision (published)