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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started