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.
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