Here program need to check whether array has a sequence of exactly 4 or more zeros in a row. and count number of sequences.
[1, 2, 0, 0, 0, 0, 2, -18, 0, 0, 0, 0, 0, 12] should return 2
If any sequence is less 4 the array is not zero plentyfull hence need to return 0 immediately
[1, 0, 0, 0, 2, 0, 0, 0, 0] should return 0
public class ZeroPlenty { public static void main(String args[]){ int a[] ={1, 0, 0, 0, 2, 0, 0, 0, 0}; System.out.println(countZeros(a)); } static int countZeros(int a[]){ int totalCount = 0; int zeroCount = 0; for(int i=0; i<a.length; i++){ if(a[i] == 0){ zeroCount++; continue; } else{ if(zeroCount >=4) totalCount++; else if(zeroCount >0 && zeroCount<4) { totalCount = 0; break; } zeroCount = 0; } } if(zeroCount >=4) totalCount++; return totalCount; } }
There was an error while loading. Please reload this page.