Nitin Agrawal
Contact -
  • Home
  • Interviews
    • Secret Receipe
    • InterviewFacts
    • Resume Thoughts
    • Daily Coding Problems
    • BigShyft
    • Companies
    • Interviews Theory
  • Programming Languages
    • Java Script >
      • Tutorials
      • Code Snippets
    • Reactive Programming >
      • Code Snippets
    • R
    • DataStructures >
      • LeetCode Problems >
        • Problem10
        • Problem300
      • AnagramsSet
    • Core Java >
      • Codility
      • Program Arguments OR VM arguments & Environment variables
      • Java Releases >
        • Java8 >
          • Performance
          • NasHorn
          • WordCount
          • Thoughts
        • Java9 >
          • ServiceLoaders
          • Lambdas
          • List Of Objects
          • Code Snippets
        • Java14 >
          • Teeing
          • Pattern
          • Semaphores
        • Java17 >
          • Switches
          • FunctionalStreams
          • Predicate
          • Consumer_Supplier
          • Collectors in Java
        • Java21 >
          • Un-named Class
          • Virtual Threads
          • Structured Concurrency
      • Threading >
        • ThreadsOrder
        • ProducerConsumer
        • Finalizer
        • RaceCondition
        • Executors
        • Future Or CompletableFuture
      • Important Points
      • Immutability
      • Dictionary
      • Sample Code Part 1 >
        • PatternLength
        • Serialization >
          • Kryo2
          • JAXB/XSD
          • XStream
        • MongoDB
        • Strings >
          • Reverse the String
          • Reverse the String in n/2 complexity
          • StringEditor
          • Reversing String
          • String Puzzle
          • Knuth Morris Pratt
          • Unique characters
          • Top N most occurring characters
          • Longest Common Subsequence
          • Longest Common Substring
        • New methods in Collections
        • MethodReferences
        • Complex Objects Comparator >
          • Performance
        • NIO >
          • NIO 2nd Sample
        • Date Converter
        • Minimum cost path
        • Find File
      • URL Validator
    • Julia
    • Python >
      • Decorators
      • String Formatting
      • Generators_Threads
      • JustLikeThat
    • Go >
      • Tutorial
      • CodeSnippet
      • Go Routine_Channel
      • Suggestions
    • Methodologies & Design Patterns >
      • Design Principles
      • Design Patterns >
        • TemplatePattern
        • Adapter Design Pattern
        • Proxy
        • Lazy Initialization
        • CombinatorPattern
        • Singleton >
          • Singletons
        • Strategy
  • Frameworks
    • Apache Velocity
    • React Library >
      • Tutorial
    • Spring >
      • Spring Boot >
        • CustomProperties
        • ExceptionHandling
        • Custom Beans
        • Issues
      • Quick View
    • Rest WebServices >
      • Interviews
      • Swagger
    • Cloudera BigData >
      • Ques_Ans
      • Hive
      • Apache Spark >
        • ApacheSpark Installation
        • SparkCode
        • Sample1
        • DataFrames
        • RDDs
        • SparkStreaming
        • SparkFiles
    • Integration >
      • Apache Camel
    • Testing Frameworks >
      • JUnit >
        • JUnit Runners
      • EasyMock
      • Mockito >
        • Page 2
      • TestNG
    • Blockchain >
      • Ethereum Smart Contract
      • Blockchain Java Example
    • Microservices >
      • Messaging Formats
      • Design Patterns
    • AWS >
      • Honeycode
    • Dockers >
      • GitBash
      • Issues
      • Kubernetes
  • Databases
    • MySql
    • Oracle >
      • Interview1
      • SQL Queries
    • Elastic Search
  • Random issues
    • TOAD issue
    • Architect's suggestions
  • Your Views
Problem : You are given an array containing 1s & 0s in random order.
    Now you need to find the maximum length of continous 1s in that array.
    You can flip some 0s to 1s & this limit you will be given as how many 0s you can flip to make them as 1.
    After fliping those many 0s, you need to find the maximum length of continuous 1s.
Example : array : 1,1,1,0,1,0,0,0,0,1
    limit : 2
    After fliping any 2 0s to get the maximum number of continous 1s, we get below -
    1,1,1,1,1,1
    So ans will be 6
Solution :
import java.util.LinkedList;
import java.util.Queue;

public class ContinuousOnes {

// static int[] numbers = {1,0,1,1,1,0,1,1,1,1,0,0,0,0,0,0,0,0,1,0,0,1,1,1,1,1,1}; //Ans : 10
// static int[] numbers = {1,0,1,1,1,0,1,0,0,0,0,0,0,0,0,1,0,0,1,1,1,1,1,1}; //Ans : 9
// static int[] numbers = {1,0,1,1,1,0,1,0,1,1,0,1,1,1,1,1,0,0,1,0,0,1,1,1,1,1,1}; //Ans : 10
// static int[] numbers = {1,0,1,1,1,0,1,0,0,1,0,1,1,1,1,1,0,0,1,1,0,1,1,1,1,1,1}; //Ans : 10
 static int[] numbers = {1,1,1,0,1,0,0,0,0,1};
 static int allowed = 2;

 public static void main(String[] args) {
  method1();
 }
 
 /**
  * It finds the answers using Queue, so needs more memory.
  */
 public static void method1() {
  Queue<Integer> queue = new LinkedList<Integer>();
  int count = allowed;
  int  max = 0;
  for(int i : numbers) {
   if(i == 1)
    queue.add(i);
   else if(count > 0) {
    queue.add(i);
    count--;
   }
   else {
    if(queue.size() > max) {
     max = queue.size();
    }
    queue.add(i);
    count--;
    while(count <= 0) {
     if(queue.peek() == 0) {
      queue.poll();
      count++;
     }
     else {
      queue.poll();
     }
    }
   }
  }
  System.out.println("Maximum length : " + (max > queue.size() ? max : queue.size()));
 }

 /**
  * It finds the answers without using any extra storage.
  */
 public static void method2() {
  int start = 0;
  int end = 0;
  int count = allowed;
  int max = 0;
  
  for(int i : numbers) {
   if(i == 1) {
    end++;
   }
   else if(count > 0) {
    end++;
    count--;
   }
   else {
    count--;
    if(max < (end-start))
     max = end - start;
    end++;
    while(count <= 0) {
     if(numbers[start] == 0) {
      count++;      
     }
     start++;
    }
   }
  }
  System.out.println("Maximum length : " + (max > (end-start) ? max : (end-start)));
 }
}
​
Improved version:
Picture
Powered by Create your own unique website with customizable templates.