Find Zeros to be Flipped so that number of Consecutive 1’s is maximized in Java

Find Zeros to be Flipped so that number of Consecutive 1’s is maximized in Java

 

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-1s-is-maximized-in-Java

Find Zeros to be Flipped so that number of Consecutive 1’s is maximized in Java

Here are the step-by-step instructions to solve this problem:

  1. Initialize two pointers left and right to the beginning of the array.
  2. 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.
  3. Move the right pointer to the right until we reach a zero. At this point, increment zero_count.
  4. 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.
  5. 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).
  6. Repeat steps 3-5 until the end of the array is reached.
  7. 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.
Find-Zeros-to-be-Flipped-so-that-number-of-Consecutive-1s-is-maximized-in Java

Program to find zeros to be flipped so that binary number is maximized in Java

Run
import java.util.*;

public class Main {
    public static void main(String[] args) {
        int[] arr = { 1, 0, 1, 0, 1, 1, 0, 1, 1, 1 };
        int n = arr.length;
        int zeros = 1;
        int max_consecutive_ones = findZeroesToFlip(arr, n, zeros);
        System.out.printf("Max consecutive ones after flipping " + zeros + " zeroes: " + max_consecutive_ones);
    }

    public static int findZeroesToFlip(int[] arr, int n, int zeros) {
        int left = 0, right = 0, zeroCount = 0, maxOnes = 0;
        while (right < n) {
            if (arr[right] == 0) {
                zeroCount++;
            }
            while (zeroCount > zeros) {
                if (arr[left] == 0) {
                    zeroCount--;
                }
                left++;
            }
            maxOnes = Math.max(maxOnes, right - left + 1);
            right++;
        }
        return maxOnes;
    }
}

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

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
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

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- CC++Java
  • Spiral Order traversal of Tree- CC++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.- CC++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- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

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
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

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- CC++Java
  • Spiral Order traversal of Tree- CC++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.- CC++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- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java