Minimum flips to make mean of all k size sub-arrays less than 1
Given an array A of size N, having each element either 0 or 1 and an integer K. Find the minimum number of elements that need to be flipped, such that no sub-array of size greater than or equal to K has an arithmetic mean of 1.
Examples:
Input: N = 5, A = {1, 1, 1, 1, 1}, K = 5
Output: 1
Explanation: Initially, mean of only sub-array of size 5 is (1+1+1+1+1)/5 = 1. So, flip the first element (i.e. make it 0). The array now becomes {0, 1, 1, 1, 1}, whose mean is less than 1. So, we needed just 1 flip to satisfy the required condition.
Note that {1, 1, 1, 1, 0} also satisfies required condition. Other arrays are also possible.Input: N = 4, A = {1, 1, 0, 1}, K = 2
Output: 1
Explanation: flip the first 1 (i.e. element at 0 index), to that resultant array becomes {0, 1, 0, 1} in which no sub-array of size 2 of more has a mean 1.
Note that {1, 0, 0, 1} is also a possible array satisfying required condition.
Approach: This problem can be easily solved by using the Greedy technique.
The observation is that a binary array of size K has an arithmetic mean equal to 1 only if all the K elements in it are equal to 1. Also, if all of the sub-arrays of size K have meant less than 1, then all sub-arrays of size greater than K would also have meant less than 1. So, the following approach can be used to solve the problem-
- Start traversing the given array.
- maintain the current count of consecutive ones till the current index in a variable, say “count”.
- If the current element is 1, we increment the count by 1, else we make it 0, as the consecutive 1s ending on ith index becomes 0.
- If the count becomes equal to K, that means there are K consecutive 1s ending on the current index, so we increment the answer by 1 (that implies the current index would be made 0 )and again make the count variable 0.
Below is the implementation of the above approach:
C++
// C++ program to find Minimum flips to // Make mean of all k size // Sub-arrays less than 1 #include <bits/stdc++.h> using namespace std; // Function to calculate // Minimum flips to make // Mean of all k size // Subarrays less than 1 int minimumFlips( int N, int A[], int K) { // Initializing answer by 0 // That stores the number of flips int answer = 0; // Initializing count variable by 0 // That stores the number of consecutive 1s int count = 0; // iterating through the array for ( int i = 0; i < N; i++) { if (A[i] == 1) { // If current element is 1, // We increment count by 1 count++; // if count of consecutive 1s // Reaches k, we increment the answer // as the mean of the subarray from // i-k to ith element becomes 1 if (count == K) { answer++; count = 0; } } // else if current element is // 0, we make count 0 else { count = 0; } } // returning the required answer return answer; } // Driver Code int main() { int N = 5, K = 5; int A[] = { 1, 1, 1, 1, 1 }; int minimum_flips = minimumFlips(N, A, K); cout << minimum_flips; } |
Java
// Java program to find Minimum flips to // Make mean of all k size // Sub-arrays less than 1 import java.io.*; class GFG { // Function to calculate // Minimum flips to make // Mean of all k size // Subarrays less than 1 static int minimumFlips( int N, int A[], int K) { // Initializing answer by 0 // That stores the number of flips int answer = 0 ; // Initializing count variable by 0 // That stores the number of consecutive 1s int count = 0 ; // iterating through the array for ( int i = 0 ; i < N; i++) { if (A[i] == 1 ) { // If current element is 1, // We increment count by 1 count++; // if count of consecutive 1s // Reaches k, we increment the answer // as the mean of the subarray from // i-k to ith element becomes 1 if (count == K) { answer++; count = 0 ; } } // else if current element is // 0, we make count 0 else { count = 0 ; } } // returning the required answer return answer; } // Driver Code public static void main (String[] args) { int N = 5 , K = 5 ; int A[] = { 1 , 1 , 1 , 1 , 1 }; int minimum_flips = minimumFlips(N, A, K); System.out.println( minimum_flips); } } // This code is contributed by hrithikgarg03188. |
Python
# Python program to find Minimum flips to # Make mean of all k size # Sub-arrays less than 1 # Function to calculate # Minimum flips to make # Mean of all k size # Subarrays less than 1 def minimumFlips(N, A, K): # Initializing answer by 0 # That stores the number of flips answer = 0 # Initializing count variable by 0 # That stores the number of consecutive 1s count = 0 # iterating through the array for i in range ( 0 , N): if (A[i] = = 1 ): # If current element is 1, # We increment count by 1 count + = 1 # if count of consecutive 1s # Reaches k, we increment the answer # as the mean of the subarray from # i-k to ith element becomes 1 if (count = = K): answer + = 1 count = 0 # else if current element is # 0, we make count 0 else : count = 0 # returning the required answer return answer # Driver Code N = 5 K = 5 A = [ 1 , 1 , 1 , 1 , 1 ] minimum_flips = minimumFlips(N, A, K) print (minimum_flips) # This code is contributed by Samim Hossain Mondal. |
C#
// C# program to find Minimum flips to // Make mean of all k size // Sub-arrays less than 1 using System; class GFG { // Function to calculate // Minimum flips to make // Mean of all k size // Subarrays less than 1 static int minimumFlips( int N, int [] A, int K) { // Initializing answer by 0 // That stores the number of flips int answer = 0; // Initializing count variable by 0 // That stores the number of consecutive 1s int count = 0; // iterating through the array for ( int i = 0; i < N; i++) { if (A[i] == 1) { // If current element is 1, // We increment count by 1 count++; // if count of consecutive 1s // Reaches k, we increment the answer // as the mean of the subarray from // i-k to ith element becomes 1 if (count == K) { answer++; count = 0; } } // else if current element is // 0, we make count 0 else { count = 0; } } // returning the required answer return answer; } // Driver Code public static void Main( string [] args) { int N = 5, K = 5; int [] A = { 1, 1, 1, 1, 1 }; int minimum_flips = minimumFlips(N, A, K); Console.WriteLine(minimum_flips); } } // This code is contributed by ukasp. |
Javascript
<script> // JavaScript code for the above approach // Function to calculate // Minimum flips to make // Mean of all k size // Subarrays less than 1 function minimumFlips(N, A, K) { // Initializing answer by 0 // That stores the number of flips let answer = 0; // Initializing count variable by 0 // That stores the number of consecutive 1s let count = 0; // iterating through the array for (let i = 0; i < N; i++) { if (A[i] == 1) { // If current element is 1, // We increment count by 1 count++; // if count of consecutive 1s // Reaches k, we increment the answer // as the mean of the subarray from // i-k to ith element becomes 1 if (count == K) { answer++; count = 0; } } // else if current element is // 0, we make count 0 else { count = 0; } } // returning the required answer return answer; } // Driver Code let N = 5, K = 5; let A = [1, 1, 1, 1, 1]; let minimum_flips = minimumFlips(N, A, K); document.write(minimum_flips); // This code is contributed by Potta Lokesh </script> |
1
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...