Find Zeros to be Flipped so that number of Consecutive 1’s is maximized in C
Find Zeros to be Flipped so that number of Consecutive 1’s is maximized in C
In this page we will look into a coding question where we will learn how to find the zeros that needs to be flipped so that the number of consecutive 1’s is maximized
There might be different approach to solve this question, one you will find here. If your approach is bit different post it onto the comment section.
Find Zeros to be Flipped so that number of Consecutive 1’s is maximized in C
Here are the step-by-step instructions to solve this problem:
- Initialize two pointers left and right to the beginning of the array.
- Initialize a variable zero_count to keep track of the number of zeroes seen so far and another variable max_ones to keep track of the maximum number of consecutive ones seen so far.
- Move the right pointer to the right until we reach a zero. At this point, increment zero_count.
- If zero_count is greater than m, move the left pointer to the right until we have flipped the required number of zeroes (i.e., zero_count – m). Update zero_count accordingly.
- Update max_ones to be the maximum of its current value and the length of the current sequence of ones (i.e., right – left + 1).
- Repeat steps 3-5 until the end of the array is reached.
- Return max_ones.
The above steps use a sliding window approach to keep track of the current sequence of ones and the number of zeroes seen so far. The left pointer is used to keep track of the start of the current sequence of ones, while the right pointer is used to move through the array. The zero_count variable is used to keep track of the number of zeroes seen so far, while max_ones is used to keep track of the maximum number of consecutive ones seen so far.
Program to find zeros to be flipped so that binary number is maximized in C
Run
#include <stdio.h> int max (int a, int b) { return a > b ? a : b; } int find_zeroes_to_flip (int arr[], int n, int zeros) { int left = 0, right = 0, zero_count = 0, max_ones = 0; while (right < n) { if (arr[right] == 0) { zero_count++; } while (zero_count > zeros) { if (arr[left] == 0) { zero_count--; } left++; } max_ones = max (max_ones, right - left + 1); right++; } return max_ones; } int main () { int arr[] = { 1, 0, 1, 0, 1, 1, 0, 1, 1, 1 }; int n = sizeof (arr) / sizeof (arr[0]); int zeros = 1; int max_consecutive_ones = find_zeroes_to_flip (arr, n, zeros); printf ("Max consecutive ones after flipping %d zeroes: %d\n", zeros, max_consecutive_ones); return 0; }
Output:
Max consecutive ones after flipping 1 zeroes: 6
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
Binary Search Trees
Traversals
- Traversal in Trees
- Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
- Tree Traversals: Depth First Search (DFS) : C | C++ | Java
- Construct a Binary Tree from Postorder and Inorder
B – Trees
AVL Trees
- AVL Trees
Complete Programs for Trees
- Depth First Traversals – C | C++ | Java
- Level Order Traversal – C | C++ | Java
- Construct Tree from given Inorder and Preorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Inorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Preorder traversal – C | C++ | Java
- Find size of the Binary tree – C | C++ | Java
- Find the height of binary tree – C | C++ | Java
- Find maximum in binary tree – C | C++ | Java
- Check whether two tree are identical- C| C++| Java
- Spiral Order traversal of Tree- C | C++| Java
- Level Order Traversal Line by Line – C | C++| Java
- Hand shaking lemma and some Impotant Tree Properties.
- Check If binary tree if Foldable or not.- C| C++| Java
- check whether tree is Symmetric – C| C++| Java.
- Check for Children-Sum in Binary Tree- C|C++| Java
- Sum of all nodes in Binary Tree- C | C++ | Java
- Lowest Common Ancestor in Binary Tree- C | C++ | Java
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
Binary Search Trees
Traversals
- Traversal in Trees
- Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
- Tree Traversals: Depth First Search (DFS) : C | C++ | Java
- Construct a Binary Tree from Postorder and Inorder
B – Trees
AVL Trees
- AVL Trees
Complete Programs for Trees
- Depth First Traversals – C | C++ | Java
- Level Order Traversal – C | C++ | Java
- Construct Tree from given Inorder and Preorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Inorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Preorder traversal – C | C++ | Java
- Find size of the Binary tree – C | C++ | Java
- Find the height of binary tree – C | C++ | Java
- Find maximum in binary tree – C | C++ | Java
- Check whether two tree are identical- C| C++| Java
- Spiral Order traversal of Tree- C | C++| Java
- Level Order Traversal LIne by Line – C | C++| Java
- Hand shaking lemma and some Impotant Tree Properties.
- Check If binary tree if Foldable or not.- C| C++| Java
- check whether tree is Symmetric C| C++| Java.
- Check for Children-Sum in Binary Tree- C|C++| Java
- Sum of all nodes in Binary Tree- C | C++ | Java
- Lowest Common Ancestor in Binary Tree. C | C++ | Java
Login/Signup to comment