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

CodeForces: Xenia and Ringroad(339B) Solution in Java for Beginners

  Xenia and Ringroad(339B) Problem Link: Since this is a beginner's problem, I suggest you try it out yourself first. Java programm...