Monday, June 12, 2017

Hackerrank Solution : The Birthday Bar / Birthday Chocolate

Problem Link

Solution Link

Explanation:

The solution is written in the getWays() method. At first we check whether n (the size of array) is less than m. If that's the case, there's no way we can satisfy Ron since we can't give him m consecutive pieces anymore. So we return 0.

Otherwise we enter a nested loop where m consecutive elements from index i to i+m are added and saved in tempSum which is evaluated to see whether it is equal to d. If it is, we have found a way.

Finally after iterating through the loops we can just print our answer that we get as a returning value from the getWays() method.

Runtime : O(n^2) since we have used a second order nested loop. 

No comments:

Post a Comment

Beginner's Guide: Some Useful Machine Learning Websites(Part 1)

I have been away for quite some time now. The reason is I was quite busy with my academic thesis and general academic pressure. Now I do fe...