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.
The o/p for the first pass is that every child with higher rating than his left neighbour has more no.of candies.
The Loop invariant can be that we are doing a safe move in giving a higher rated child 1 more candy than his left sided neighbour, because
1> the prob requires it to be done so
2> This the minimum possible increase (hence it is part of the optimal soln). Any other number of toffees must be >= this number.
The same argument can be given for your right sided pass
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Candies
You are viewing a single comment's thread. Return to all comments →
The o/p for the first pass is that every child with higher rating than his left neighbour has more no.of candies. The Loop invariant can be that we are doing a safe move in giving a higher rated child 1 more candy than his left sided neighbour, because 1> the prob requires it to be done so 2> This the minimum possible increase (hence it is part of the optimal soln). Any other number of toffees must be >= this number.
The same argument can be given for your right sided pass