Suppose you are given an array of n integers and an integer k (n<= 10^5, 1<=k<=n). How to find the sub-array(contiguous) with maximum average whose length is more than k.

Thanks in advance.

Codeforces celebrates 10 years! We are pleased to announce the crowdfunding-campaign. Congratulate us by the link https://codeforces.com/10years.
×

Before contest

Codeforces Round #625 (Div. 1, based on Technocup 2020 Final Round)

26:01:49

Register now »

Codeforces Round #625 (Div. 1, based on Technocup 2020 Final Round)

26:01:49

Register now »

*has extra registration

Before contest

Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round)

26:01:49

Register now »

Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round)

26:01:49

Register now »

*has extra registration

# | User | Rating |
---|---|---|

1 | MiFaFaOvO | 3681 |

2 | tourist | 3496 |

3 | apiadu | 3438 |

4 | TLE | 3356 |

5 | LHiC | 3339 |

6 | mnbvmar | 3281 |

7 | 300iq | 3267 |

8 | Radewoosh | 3242 |

9 | yosupo | 3233 |

10 | 1919810 | 3203 |

# | User | Contrib. |
---|---|---|

1 | Errichto | 190 |

2 | antontrygubO_o | 186 |

3 | tourist | 182 |

4 | Radewoosh | 168 |

5 | vovuh | 167 |

6 | pikmike | 165 |

7 | ko_osaga | 161 |

7 | Um_nik | 161 |

9 | rng_58 | 156 |

10 | Petr | 154 |

Suppose you are given an array of n integers and an integer k (n<= 10^5, 1<=k<=n). How to find the sub-array(contiguous) with maximum average whose length is more than k.

Thanks in advance.

↑

↓

Codeforces (c) Copyright 2010-2020 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Feb/29/2020 14:03:11 (e1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

first we do a binary search on the average and let it be x

we decrease x from all of the array elements and if there exists a sub array with lengh more than k whose sum is more than zero then we can say that we have such a sub array whose average is more than x other wise we can say that there doesnt exist any such sub array

how to find out if there is a sub array whose sum is more than zero and its length is more than k? we can say that a sub array [l, r) equals sum[1, r) — sum[1, l) so if we get the partial sums and fix the r of the sub array we just need an l which sum[1, r) >= sum[1, l) and l <= r — k this can be done with partial minimum of the partial sums

Your idea seems excellent :) thank you :)

Can it be done in O(n) complexity?

there isn't any way that i know of !

if anybody knows anything please say ;D

It's possible: http://www.cs.cornell.edu/~chung/download/density.pdf

Tnx :) But the article seems difficult to me :( If u know the algorithm can you please explain :/

It would be nice if u can write the crux of article here .That would be helpfull .

I'll write a blog post about this topic when I have time for it.

The link is not working. Do you have any other references?

Coincidence?

That question is not even based on this!

So you participated in the contest but this wasn't based on it? — http://store.picbg.net/pubpic/B3/4C/eb16def031d2b34c.png

Yes I did participate. And yes it is not based on the sum.

Another link: http://arxiv.org/abs/cs/0311020

Thank you!

https://www.geeksforgeeks.org/longest-subarray-having-average-greater-than-or-equal-to-x/