Question: Write a program that finds a contiguous subarray of length at most (m) in an array of (n) long integers that has the highest average
Write a program that finds a contiguous subarray of length at most \(m\) in an array of \(n\) long integers that has the highest average value among all such subarrays, by trying all subarrays. Use the scientific method to confirm that the order of growth of the running time of your program is \(m n^{2}\). Next, write a program that solves the problem by first computing the quantity prefix[i] \(=a[0]+\ldots+a[i]\) for each \(i\), then computing the average in the interval from \(a[i]\) to \(a[j]\) with the expression (prefix[j] - prefix[i]) / (j-i+1). Use the scientific method to confirm that this method reduces the order of growth by a factor of \(n\).
Step by Step Solution
3.37 Rating (153 Votes )
There are 3 Steps involved in it
Great lets start by writing both types of programs First lets write a program that tries all subarrays then well evaluate its performance python impor... View full answer
Get step-by-step solutions from verified subject matter experts
