Goodland Electricity

  • + 1 comment

    My Code:

    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
            int k=sc.nextInt();
            int a[]=new int[n];
            for(int i=0;i<n;i++)
                a[i]=sc.nextInt();
            int start=0; int count=0;
            while(start<n){
            int j=k-1;
                if((start+j)>=n)
                    j=n-start-1;
            while((start+j)>=0 && a[start+j]!=1 ){
                j--;
            }
               if(j>k || j<(-k+1) || (start+j)==(-1)){
                  count=-1; break;}
            else{
            start=start+j+k;
                count++;}
            }
            System.out.println(count);
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        }
    }