Given a binary array arr[] consisting of N integers, the task is to calculate the minimum number of operations of the following type needed to convert all elements of the array equal to K:
- Select any index X from the given array.
- Flip all the elements of the subarray arr[X] … arr[N – 1], i.e., if arr[i] = 1, then set arr[i] as 0 and vice versa.
Examples:
Input: N = 8, arr[ ] = {1, 0, 1, 0, 0, 1, 1, 1}, K = 0
Output:5
Explanation:
Operation 1: X = 0 (chosen index). After modifying values the updated array arr[] is [0, 1, 0, 1, 1, 0, 0, 0].
Operation 2: X = 1 (chosen index). After modifying values the updated array arr[] is [0, 0, 1, 0, 0, 1, 1, 1].
Operation 3: X = 2 (chosen index). After modifying values the updated array arr[] is [0, 0, 0, 1, 1, 0, 0, 0].
Operation 4: X = 3 (chosen index). After modifying values the updated array arr[] is [0, 0, 0, 0, 0, 1, 1, 1].
Operation 5: X = 5 (chosen index). After modifying values the updated array arr[] is [0, 0, 0, 0, 0, 0, 0, 0].Input: N = 8, arr[ ] = {1, 0, 1, 0, 0, 1, 1, 1}, K = 1
Output:4
Approach:The following observations is to be made:
As any index X ( < N) can be chosen and each value from index X to the index N-1 can be modified, so it can be found that the approach is to count the number of changing points in the array.
Follow the steps below to solve the above problem:
- Initialize a variable flag to inverse of K. i.e. 1 if K = 0 or vice versa, which denotes the current value.
- Initialize a variable cnt to 0, that keeps the count of the number of changing points in the array arr[].
- Traverse the array arr[] and for each index i apply the following steps:
- If the flag value and arr[i] value are different, go to the next iteration.
- If both the flag and arr[i] are equal, increase count and set flag to flag = (flag + 1) % 2.
- Print the final value of count.
Below is the implementation of the above approach:
C++14
// C++14 Program to implement // the above appraoch #include <bits/stdc++.h> using namespace std; // Function to count the minimum // number of subarray flips required int minSteps(int arr[], int n, int k) { int i, cnt = 0; int flag; if (k == 1) flag = 0; else flag = 1; // Iterate the array for (i = 0; i < n; i++) { // If arr[i] and flag are equal if (arr[i] == flag) { cnt++; flag = (flag + 1) % 2; } } // Return the answer return cnt; } // Driver Code int main() { int arr[] = { 1, 0, 1, 0, 0, 1, 1, 1 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 1; cout << minSteps(arr, n, k); return 0; } |
Java
// Java program to implement // the above approach import java.util.*; class GFG{ // Function to count the minimum // number of subarray flips required static int minSteps(int arr[], int n, int k) { int i, cnt = 0; int flag; if (k == 1) flag = 0; else flag = 1; // Iterate the array for(i = 0; i < n; i++) { // If arr[i] and flag are equal if (arr[i] == flag) { cnt++; flag = (flag + 1) % 2; } } // Return the answer return cnt; } // Driver code public static void main (String[] args) { int arr[] = { 1, 0, 1, 0, 0, 1, 1, 1 }; int n = arr.length; int k = 1; System.out.print(minSteps(arr, n, k)); } } // This code is contributed by offbeat |
Python3
# Python3 program to implement # the above approach # Function to count the minimum # number of subarray flips required def minSteps(arr, n, k): cnt = 0 if(k == 1): flag = 0 else: flag = 1 # Iterate the array for i in range(n): # If arr[i] and flag are equal if(arr[i] == flag): cnt += 1 flag = (flag + 1) % 2 # Return the answer return cnt # Driver Code arr = [ 1, 0, 1, 0, 0, 1, 1, 1 ] n = len(arr) k = 1 # Function call print(minSteps(arr, n, k)) # This code is contributed by Shivam Singh |
C#
// C# program to implement // the above approach using System; class GFG{ // Function to count the minimum // number of subarray flips required static int minSteps(int[] arr, int n, int k) { int i, cnt = 0; int flag; if (k == 1) flag = 0; else flag = 1; // Iterate the array for(i = 0; i < n; i++) { // If arr[i] and flag are equal if (arr[i] == flag) { cnt++; flag = (flag + 1) % 2; } } // Return the answer return cnt; } // Driver code public static void Main () { int[] arr = { 1, 0, 1, 0, 0, 1, 1, 1 }; int n = arr.Length; int k = 1; Console.Write(minSteps(arr, n, k)); } } // This code is contributed by chitranayal |
4
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum substring flips required to convert given binary string to another
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum non-adjacent pair flips required to remove all 0s from a Binary String
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Minimum Group Flips to Make Binary Array Elements Same
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Minimum number of flips required such that the last cell of matrix can be reached from any other cell
- Minimum flips in two binary arrays so that their XOR is equal to another array
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Count minimum right flips to set all values in an array
- Minimum subarray reversals required such that sum of all pairs of adjacent elements is odd
- Minimum number of flips with rotation to make binary string alternating
- Minimize count of flips required such that no substring of 0s have length exceeding K
- Minimum flips of odd indexed elements from odd length subarrays to make two given arrays equal
- Minimum flips to make all 1s in left and 0s in right | Set 2
- Minimum flips to make all 1s in left and 0s in right | Set 1 (Using Bitmask)
- Minimum flips to make all 1s in right and 0s in left
- Minimize subarray increments/decrements required to reduce all array elements to 0
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

