Minimum flips required to convert given string into concatenation of equal substrings of length K Last Updated : 28 May, 2021 Comments Improve Suggest changes Like Article Like Report Given a binary string S and an integer K, the task is to find the minimum number of flips required to convert the given string into a concatenation of K-length equal sub-strings. It is given that the given string can be split into K-length substrings. Examples: Input: S = "101100101", K = 3 Output: 1 Explanation: Flip the '0' from index 5 to '1'. The resultant string is S = "101101101". It is the concatenation of substring "101". Hence, the minimum number of flips required is 1. Input: S = "10110111", K = 4 Output: 2 Explanation: Flip the '0' and '1' at indexes 4 and 5 respectively. The resultant string is S = "10111011". It is the concatenation of the substring "1011". Hence, the minimum number of flips required is 2. Approach: The problem can be solved using Greedy Approach. Follow the steps below: Iterate the given string with increments of K indices from each index and keep a count of the 0s and 1s.The character which occurs the minimum number of times must be flipped and keep incrementing that count.Perform the above steps for all the indices from 0 to K-1 to obtain the minimum number of flips required. Below is the implementation of the above approach: C++ // C++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function that returns the minimum // number of flips to convert // the s into a concatenation // of K-length sub-string int minOperations(string S, int K) { // Stores the result int ans = 0; // Iterate through string index for (int i = 0; i < K; i++) { // Stores count of 0s & 1s int zero = 0, one = 0; // Iterate making K jumps for (int j = i; j < S.size(); j += K) { // Count 0's if (S[j] == '0') zero++; // Count 1's else one++; } // Add minimum flips // for index i ans += min(zero, one); } // Return minimum number // of flips return ans; } // Driver Code int main() { string S = "110100101"; int K = 3; cout << minOperations(S, K); return 0; } Java // Java program to implement // the above approach import java.io.*; class GFG{ // Function that returns the minimum // number of flips to convert // the s into a concatenation // of K-length sub-string public static int minOperations(String S, int K) { // Stores the result int ans = 0; // Iterate through string index for(int i = 0; i < K; i++) { // Stores count of 0s & 1s int zero = 0, one = 0; // Iterate making K jumps for(int j = i; j < S.length(); j += K) { // Count 0's if (S.charAt(j) == '0') zero++; // Count 1's else one++; } // Add minimum flips // for index i ans += Math.min(zero, one); } // Return minimum number // of flips return ans; } // Driver Code public static void main(String args[]) { String S = "110100101"; int K = 3; System.out.println(minOperations(S, K)); } } // This code is contributed by grand_master Python3 # Python3 program to implement # the above approach # Function that returns the minimum # number of flips to convert the s # into a concatenation of K-length # sub-string def minOperations(S, K): # Stores the result ans = 0 # Iterate through string index for i in range(K): # Stores count of 0s & 1s zero, one = 0, 0 # Iterate making K jumps for j in range(i, len(S), K): # Count 0's if(S[j] == '0'): zero += 1 # Count 1's else: one += 1 # Add minimum flips # for index i ans += min(zero, one) # Return minimum number # of flips return ans # Driver code if __name__ == '__main__': s = "110100101" K = 3 print(minOperations(s, K)) # This code is contributed by Shivam Singh C# // C# program to implement // the above approach using System; class GFG{ // Function that returns the minimum // number of flips to convert // the s into a concatenation // of K-length sub-string public static int minOperations(String S, int K) { // Stores the result int ans = 0; // Iterate through string index for(int i = 0; i < K; i++) { // Stores count of 0s & 1s int zero = 0, one = 0; // Iterate making K jumps for(int j = i; j < S.Length; j += K) { // Count 0's if (S[j] == '0') zero++; // Count 1's else one++; } // Add minimum flips // for index i ans += Math.Min(zero, one); } // Return minimum number // of flips return ans; } // Driver Code public static void Main(String []args) { String S = "110100101"; int K = 3; Console.WriteLine(minOperations(S, K)); } } // This code is contributed by 29AjayKumar JavaScript <script> // JavaScript program to implement // the above approach // Function that returns the minimum // number of flips to convert // the s into a concatenation // of K-length sub-string function minOperations(S, K) { // Stores the result var ans = 0; // Iterate through string index for (var i = 0; i < K; i++) { // Stores count of 0s & 1s var zero = 0, one = 0; // Iterate making K jumps for (var j = i; j < S.length; j += K) { // Count 0's if (S[j] === "0") zero++; // Count 1's else one++; } // Add minimum flips // for index i ans += Math.min(zero, one); } // Return minimum number // of flips return ans; } // Driver Code var S = "110100101"; var K = 3; document.write(minOperations(S, K)); </script> Output: 2 Time Complexity: O(N) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Minimum flips required to convert given string into concatenation of equal substrings of length K R rutvik_56 Follow Improve Article Tags : Misc Strings Greedy Pattern Searching Searching Mathematical DSA binary-string substring +5 More Practice Tags : GreedyMathematicalMiscPattern SearchingSearchingStrings +2 More Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e 7 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example 12 min read Like