Question: (20 points) Your friend, John, now gives you an array B[1..n] that contains only positive integers. He wants to know if there is a contiguous
![(20 points) Your friend, John, now gives you an array B[1..n]](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f3ca38e2495_04866f3ca386db38.jpg)
(20 points) Your friend, John, now gives you an array B[1..n] that contains only positive integers. He wants to know if there is a contiguous portion of the array, B[i..j], such that the sum of all its entries is equal to the number Y. One way to solve this is: For each pair of (i,j), compute the sum of all entries in B[i..j] and check if it is equal to Y. Unfortunately, this algorithm is very inefficient, as there are (n2) sums to test, and each sum involves O(n) addition operations. A careful analysis show that it will take (n3) running time in total. Design an O(n)-time algorithm to help John find the desired portion B[i..j], and explain why it runs in O(n) time. Also briefly show the correctness of your algorithm. Note: If your algorithm does not run in O(n) time, but runs in O(n2) time, your maximum score for this question is 10 points (instead of 20). Otherwise, if it runs in (n2) time, your score is 0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
