Hi All,
Does anyone know where my approach will fail ?
I went through the whole string from left to write checking required number of days(subarrays). While doing this, I was checking if a subarray has a single one and if, single one is encountered first time in a subarray, then I am changing it to 0 and making it a vacation.
This approach went fine with 1st, and 4th subtask but gave wrong answer for 2nd and 3rd subtask. No idea why. Can anyone give an example where this would fail.
Following is the code that I used.
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int i=0;i<T;i++){
int N = sc.nextInt();
int X = sc.nextInt();
sc.nextLine();
String s = sc.nextLine();
Object result = getVacations(N,X,s);
System.out.println(result);
}
}
public static Object getVacations(int N,int X,String s){
int count = 0;
boolean flag = false;
for(int i=0;i<N;i++){
int temp=0;
for(int j=i;j<i+X && j<N;j++){
if(s.charAt(j)=='0'){
temp++;
}
}
if(temp==X){
count++;
i+=(X-1);
}else if((X-temp)==1 && !flag){
count++;
flag=true;
i+=(X-1);
}
}
return count;
}
}