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:
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: