Minimum flips to make mean of all k size sub-arrays less than 1
Last Updated :
21 Sep, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int minimumFlips( int N, int A[], int K)
{
int answer = 0;
int count = 0;
for ( int i = 0; i < N; i++) {
if (A[i] == 1) {
count++;
if (count == K) {
answer++;
count = 0;
}
}
else {
count = 0;
}
}
return answer;
}
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
import java.io.*;
class GFG {
static int minimumFlips( int N, int A[], int K)
{
int answer = 0 ;
int count = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (A[i] == 1 )
{
count++;
if (count == K) {
answer++;
count = 0 ;
}
}
else {
count = 0 ;
}
}
return answer;
}
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);
}
}
|
Python
def minimumFlips(N, A, K):
answer = 0
count = 0
for i in range ( 0 , N):
if (A[i] = = 1 ):
count + = 1
if (count = = K):
answer + = 1
count = 0
else :
count = 0
return answer
N = 5
K = 5
A = [ 1 , 1 , 1 , 1 , 1 ]
minimum_flips = minimumFlips(N, A, K)
print (minimum_flips)
|
C#
using System;
class GFG {
static int minimumFlips( int N, int [] A, int K)
{
int answer = 0;
int count = 0;
for ( int i = 0; i < N; i++) {
if (A[i] == 1) {
count++;
if (count == K) {
answer++;
count = 0;
}
}
else {
count = 0;
}
}
return answer;
}
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);
}
}
|
Javascript
<script>
function minimumFlips(N, A, K)
{
let answer = 0;
let count = 0;
for (let i = 0; i < N; i++) {
if (A[i] == 1) {
count++;
if (count == K) {
answer++;
count = 0;
}
}
else {
count = 0;
}
}
return answer;
}
let N = 5, K = 5;
let A = [1, 1, 1, 1, 1];
let minimum_flips = minimumFlips(N, A, K);
document.write(minimum_flips);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Another Approach:
- Define the function minimumFlips that takes in an integer N, an integer array A of size N, and an integer K as input parameters and returns an integer as the output.
- Initialize two variables answer and sum to 0.
- Traverse through the first K-1 elements of the array and add their values to sum.
- Traverse through the remaining elements of the array from K to N-1.
- Add the current element to sum.
- Check if the arithmetic mean of the K consecutive elements starting from the current element is greater than or equal to 1.
- If it is, increment answer by 1 and decrement sum by 1 (as we need to flip the current element to 0).
- Check if the index i-K+1 is greater than or equal to 0 (to ensure that we do not access out of bounds elements).
- If it is, subtract the value of the element at index i-K+1 from sum.
- Return the value of answer as the output.
C++
#include<bits/stdc++.h>
using namespace std;
int minimumFlips( int N, int A[], int K) {
int answer = 0, sum = 0;
for ( int i = 0; i < K - 1; i++) {
sum += A[i];
}
for ( int i = K - 1; i < N; i++) {
sum += A[i];
if (( double )sum/K >= 1) {
answer++;
sum--;
}
if (i - K + 1 >= 0) {
sum -= A[i - K + 1];
}
}
return answer;
}
int main() {
int N = 5, K = 5;
int A[] = { 1, 1, 1, 1, 1 };
int minimum_flips = minimumFlips(N, A, K);
cout << minimum_flips << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int minimumFlips( int N, int A[], int K) {
int answer = 0 , sum = 0 ;
for ( int i = 0 ; i < K - 1 ; i++) {
sum += A[i];
}
for ( int i = K - 1 ; i < N; i++) {
sum += A[i];
if (( double ) sum / K >= 1 ) {
answer++;
sum--;
}
if (i - K + 1 >= 0 ) {
sum -= A[i - K + 1 ];
}
}
return answer;
}
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);
}
}
|
Python
def minimumFlips(N, A, K):
answer = 0
sum_val = 0
for i in range (K - 1 ):
sum_val + = A[i]
for i in range (K - 1 , N):
sum_val + = A[i]
if sum_val / K > = 1 :
answer + = 1
sum_val - = 1
if i - K + 1 > = 0 :
sum_val - = A[i - K + 1 ]
return answer
N = 5
K = 5
A = [ 1 , 1 , 1 , 1 , 1 ]
minimum_flips = minimumFlips(N, A, K)
print (minimum_flips)
|
C#
using System;
public class MainClass
{
public static int MinimumFlips( int N, int [] A, int K)
{
int answer = 0;
int sum = 0;
for ( int i = 0; i < K - 1; i++)
{
sum += A[i];
}
for ( int i = K - 1; i < N; i++)
{
sum += A[i];
if (( double )sum / K >= 1)
{
answer++;
sum--;
}
if (i - K + 1 >= 0)
{
sum -= A[i - K + 1];
}
}
return answer;
}
public static void Main( string [] args)
{
int N = 5;
int K = 5;
int [] A = { 1, 1, 1, 1, 1 };
int minimumFlips = MinimumFlips(N, A, K);
Console.WriteLine(minimumFlips);
}
}
|
Javascript
function minimumFlips(N, A, K) {
let answer = 0, sum = 0;
for (let i = 0; i < K - 1; i++) {
sum += A[i];
}
for (let i = K - 1; i < N; i++) {
sum += A[i];
if (sum/K >= 1) {
answer++;
sum--;
}
if (i - K + 1 >= 0) {
sum -= A[i - K + 1];
}
}
return answer;
}
let N = 5, K = 5;
let A = [ 1, 1, 1, 1, 1 ];
let minimum_flips = minimumFlips(N, A, K);
console.log(minimum_flips);
|
Time complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...