Answered step by step
Verified Expert Solution
Question
1 Approved Answer
(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] 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
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