Average Subarray
Practice
3.9 (8 votes)
Algorithms
Basics of greedy algorithms
Greedy algorithms
Problem
91% Success 1869 Attempts 20 Points 5s Time Limit 256MB Memory 1024 KB Max Code

You are given an binary array A of N integers. You are also given an integer K and you need to ensure that no subarray of size greater than or equal to K has average 1.
For this you can perform the below operation:

  • Choose any index and flip the bit.

Find the minimum number of operations to acheive the above condition.

Input:

First line contains number of test cases T. For each test case:

  • First line contains two space seperated integers N and K.
  • Second line contains N space separated integers of the binary array.

Output:

Print T lines , one for each test case. For each test case:

  • Print a single line denoting the minimum number of operations that needs to be performed.

Constraints:

  • \(1 \leq T \leq 20000\)
  • \(1 \leq N \leq 200000\)
  • \(1 \leq K \leq N\)
  • \( 0 \leq A_i \leq 1\)
  • Sum of N over all test cases will not exceed 200000.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
15 votes
Tags:
AlgorithmsApprovedEasyGreedy Algorithms
Points:20
8 votes
Tags:
AlgorithmsEasyGreedy AlgorithmsImplementationString Manipulation
Points:30
9 votes
Tags:
MediumAlgorithms