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

Interview Questions at Enosis(Part 3)

In Part 2 , I have discussed 3 coding problems out of 6. Here we will talk about the next 3 coding problems. Problem 4: Write a function...