• + 2 comments

    I have similar solution, only difference is start of count. Its logic is as follows:

    • 1) loop through 0 to n-2
    • 2) if you can jump 2 times (c[i+2]==0) then jump by increasing i, and no of hops.
    • 3) if not, simply increase hops, (i will be incremented by for loop)
    • 4) If we didn't reach end of loop (n-1), then increase hop by 1, because we are at second last (n-2), and last cloud is always ordinary cloud and it can be jumped.
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int c[] = new int[n];
        for(int i=0; i < n; i++){
            c[i] = in.nextInt();
        }
    
        int hops=0,i;
        for(i=0;i<n-2;i++){
            if (c[i+2]==0){
                i++;
                hops++;
                continue;
            }
            hops++;
        }
        if(i<n-1)
            hops++;
        System.out.println(hops);
    }