Subarray Division Algorithm

Writing about things I learn helps me understand better, and I hope reading them will help you too! π
In this article, I'll be describing how I solved the
Subarray DivisionChallenge on HackerRank using Python 3.
This challenge is part of the Implementation challenges in the Algorithm section on HackerRank.
Problem π²
Two children, Lily and Ron, want to share a chocolate bar. Each of the squares has an integer on it.
Lily decides to share a contiguous segment of the bar selected such that:
- The length of the segment matches Ron's birth month, and,
- The sum of the integers on the squares is equal to his birthday.
Determine how many ways she can divide the chocolate.
Example
Samle Input 0:
s = [2, 2, 1, 3, 2]
d = 4
m = 2
Samle Ouput 0:
2
Explanation 0
Lily wants to find segments summing to Ron's birthday, d = 4 with a length equalling his birth month, m = 2. In this case, there are two segments meeting her criteria: [2, 2] and [1, 3].
Function Description
Complete the birthday function in the editor below. birthday has the following parameter(s):
int s[n]: the numbers on each of the squares of chocolateint d: Ron's birth dayint m: Ron's birth month
Samle Input 1:
1 2 1 3 2
Samle Ouput 1:
2
Explanation 1
Lily wants to give Ron m = 2 squares summing to d = 3 . The following two segments meet the criteria:

I'll get right into the algorithm. Please feel free to read the full question on HackerRank.
Solution π
Let's consider Sample Input 1

In plain English, there's a chocolate bar, and this bar is made up of squares. We need to count how many times Lily can divide this chocolate bar such that all
mconsecutive squares sum up tod.
Now we've understood the problem, how do we code this out? π€π€
Step 1οΈβ£: Slice the chocolate bar based on m. π«πͺπ«
- We first need to slice the chocolate bar based on a given number of squares. this number is
m. - This slicing has to be done consecutively. Since we're iterating through a list, I'll use list comprehensions because Pythonπ.
- We'll store this new sliced chocolate bar in a variable
bar
bar = [ s[i:i + m] for i in range(0, len(s)) ]
This code means for every square in the chocolate bar, slice from the ith square to the (i+m)th square and then return these slices in a list called bar.
Step 2οΈβ£: Loop through the slices and compare each length with d.
- Now we've gotten a list of consecutive sliced segments. \
- The next step is to iterate through these segments, while calculating their respective lengths
- Return a list
seg_sumwith the sum of all consecutive segments.
seg_sum = [ sum(seg) for seg in bar ]
Step 3οΈβ£: Count whenever each sliced segment equals d.
- Now we have a new list of the segment sums, we need to compare each sum to
d. - Initialise a
countervariable to track how many times this condition occurs. - Return
counterin a list whenever this condition occurs. - Finally, print the sum of this new list
result, in other words, count how many timescounterwas returned.
counter = 1
result = [ counter for seg in seg_sum if seg == d ]
Full code:
def birthday(s, d, m):
# Write your code here
counter = 1
bar = [ s[i:i + m] for i in range(0, len(s)) ]
seg_sum = [ sum(seg) for seg in bar ]
result = [ counter for seg in seg_sum if seg == d ]
return sum(result)
birthday([1, 2, 1, 3, 2], 3, 2)
>> 2
Hope this helps with understanding this challenge. Happy Coding!π¨π½βπ»





