We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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);
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Jumping on the Clouds
You are viewing a single comment's thread. Return to all comments →
I have similar solution, only difference is start of count. Its logic is as follows:
0
ton-2
(c[i+2]==0)
then jump by increasingi
, and no ofhops
.(n-1)
, then increase hop by1
, because we are at second last(n-2)
, and last cloud is always ordinary cloud and it can be jumped.