Question: Finds the shortest sequence of consecutive array elements that add up to * the specified sum and returns the length of that sequence. * If

Finds the shortest sequence of consecutive array elements that add up to * the specified sum and returns the length of that sequence. * If no such sequence is found, the method returns 0. *

* E.g., [2, 10, 4, 8, 1, 22, 5, 16, 2] and sum = 23 returns 2 * This example has three sequences that add up to 23: 10 + 4 + 8 + 1, 1 + 22, and 5 + 16 + 2. * Since 1 + 22 is the shortest, the method returns 2. * E.g., [1, -7, 4, 0] and sum = -2 returns 3 because 1 - 7 + 4 equals -2 *

* Boundary cases: * If the array is empty or if the user passes null as the first argument, the * method should return 0. No exception should be thrown. * * @param array * @param sum * @return the minimum number of consecutive addends to calculate the specified sum. */

also [5] and sum 5 should return 1, [3,2,4,5,6] and sum 3 should return 1 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!