Subarray Division Algorithm

Photo by Tamas Pap on Unsplash

Subarray Division Algorithm

Β·

3 min read

In this article, I'll be describing how I solved the Subarray Division Challenge 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 chocolate
  • int d: Ron's birth day
  • int 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: image.png

I'll get right into the algorithm. Please feel free to read the full question on HackerRank.

Solution πŸ”Ž

Let's consider Sample Input 1 image.png

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 m consecutive squares sum up to d.

Now we've understood the problem, how do we code this out? πŸ€”πŸ€”

Step 1️⃣: Slice the chocolate bar based on m. 🍫πŸ”ͺ🍫

  1. We first need to slice the chocolate bar based on a given number of squares. this number is m.
  2. This slicing has to be done consecutively. Since we're iterating through a list, I'll use list comprehensions because Python😏.
  3. 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.

  1. Now we've gotten a list of consecutive sliced segments. \
  2. The next step is to iterate through these segments, while calculating their respective lengths
  3. Return a list seg_sum with the sum of all consecutive segments.
seg_sum = [ sum(seg) for seg in bar ]

Step 3️⃣: Count whenever each sliced segment equals d.

  1. Now we have a new list of the segment sums, we need to compare each sum to d.
  2. Initialise a counter variable to track how many times this condition occurs.
  3. Returncounter in a list whenever this condition occurs.
  4. Finally, print the sum of this new list result, in other words, count how many times counter was 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!πŸ‘¨πŸ½β€πŸ’»

Buy me a coffee.png

Β