Lava Interview Question
Front-end Software EngineersCountry: India
Interview Type: Written Test
public static int NumberOfMaxOccurences(int[] input, int k) {
int flippedZeroCount = 0;
int maxLengthFound = 0;
int maxCount = 0;
int leftPos = 0;
int rightPos = 0;
if(input.length == 0){
return 0;
}
if(input.length == 1){
if(flippedZeroCount == 0){
return input[0]==1?1:0;
}
if(flippedZeroCount == 1){
return input[0]==1?0:1;
}
}
while(rightPos<input.length){
if(input[rightPos] == 0 ){
flippedZeroCount++;
}
if(flippedZeroCount == k){
while(rightPos<input.length-1 && input[rightPos+1] == 1){
rightPos++;
}
if(rightPos-leftPos+1 > maxLengthFound){
maxLengthFound = rightPos-leftPos + 1;
maxCount = 1;
}else if(rightPos-leftPos + 1 == maxLengthFound){
maxCount++;
}
}
if(input[leftPos++] == 0 ){
flippedZeroCount--;
}
rightPos++;
}
return maxCount;
}
}
- Bharat March 01, 2018