Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

Feature #6: Find Two Sets of Consecutive Days

Sumit Rawal answered on May 24, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers


Feature #6: Find Two Sets of Consecutive Days

0

To implement this solution, we can calculate the prefix sum for each index in the input array and use a hash table to store it. Then, we traverse the array and use the hash table to find the shortest subarrays with a sum equal to k.

The complete algorithm is given below:

First, we will traverse through the input array once and store the key-value pairs in a hash table such that, for every ith index, the key is the sum of hoursPerDay[0] to hoursPerDay[i] and the value is i.

We will also add (0, -1) to the hash table as the default.

Now, we will traverse through the array again, and for every i, we will find the minimum value of the length of the subarray on the left or starting with i, whose value is equal to k.

Then, we will find another subarray starting with i + 1, whose sum is k.

We will update the result with the minimum value of the sum of both the subarrays. This is possible because all the values are positive, and the value of the sum is strictly increasing. 

Popularity 1/10 Helpfulness 1/10 Language typescript
Source: Grepper
Link to this answer
Share Copy Link
Contributed on May 24 2023
Sumit Rawal
0 Answers  Avg Quality 2/10


X

Continue with Google

By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
X
Grepper Account Login Required

Oops, You will need to install Grepper and log-in to perform this action.