Question

In: Computer Science

Given an array ? of numbers and a window size ?, the sliding window ending at...

Given an array ? of numbers and a window size ?, the sliding window ending at index ? is the subarray ?[? − ? + 1], ⋯ , ?[?] if ? ≥ ? − 1, and ?[0], ⋯ , ?[?] otherwise. For example, if ? = [0, 2, 4, 6, 8] and ? = 3, then the sliding windows ending at indices 0, 1, 2, 3 and 4 are respectively [0], [0, 2], [0, 2, 4], [2, 4, 6], and [4, 6, 8]. Write a method, movingAverage, that given as input an array ? of numbers and a window size ?, returns an array ? with the same length as ?, such that for every index ?, ?[?] is the average of the numbers in the sliding window ending at index ?. The skeleton for the method is provided in the file MovingAverage.java.

The following is a sample run.

Input:      ? = [0,2, 4, 6, 8],   ? = 3

Return:   [0, 1, 2, 4, 6]

Explanation: As explained above, the sliding windows at indices 0, 1, 2, 3 and 4 are respectively

[0], [0,2], [0, 2, 4], [2, 4, 6], and [4, 6, 8]. The average of the numbers in these sliding windows are respectively = 0, = 1, = 2, = 4, and = 6.

Your method must have time complexity ?(?), where ? is the length of the input array ?.

Hint: You may find a queue useful.

public class MovingAverage {


   public double[] movingAverage(int[] A, int w) {
      
       //Replace this line with your return statement
       return null;
   }

}

Solutions

Expert Solution


        public static double[] movingAverage(int[] A, int w) {
                
                double result[] = new double[A.length];
                
                double currentSum = 0;
                for(int i=0; i<A.length; i++) {
                        currentSum += A[i];
                        
                        if(i >= w) {
                                currentSum -= A[i-w];
                        }
                        
                        int len = Math.min(i + 1, w);
                        result[i] = currentSum/len;
                }

                // Replace this line with your return statement
                return result;
        }
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

. As input you are given two arrays: an array of numbers ? and an array...
. As input you are given two arrays: an array of numbers ? and an array ? of queries where each query is a target number. The array ? is unsorted and may contain duplicates. Your goal is, for each query ? in the array ?, count the number of pairs in the array ? that sums up to ?; that is, the number of distinct pairs of indices [?, ?], with ? < ?, such that ?[?] + ?[?]...
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
//   Given an array of size 9, with the values of 1-9, determine if the array...
//   Given an array of size 9, with the values of 1-9, determine if the array //   is valid or not. //   Display a message stating the row is VALId, or state its INVALID and what //   was the index number that determined the data invalid. // //   Use this java code as a start of your code. //   Test the array data by changing the values. //============================================================================= import java.util.*;    public class Soduko_ValidateRow    { public static void main(String...
Given an array A of n distinct real numbers, we say that a pair of numbers...
Given an array A of n distinct real numbers, we say that a pair of numbers i, j ∈ {0, . . . , n−1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Answer the following: (a) How small can the number of inversions be? Give an example of an array of length n with the smallest possible number of inversions. (b)...
Given an array A of n distinct numbers, we say that a pair of numbers i,...
Given an array A of n distinct numbers, we say that a pair of numbers i, j ∈ {0, . . . , n − 1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Define the Inversion problem as follows: • Input: an array A consisting of distinct numbers. • Output: the number of inversions of A, i.e. |inv(A)|. Answer the following:...
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N...
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax (no [ ]’s or (ptr+i) stuff.     Input will be the number of input values to enter followed by the sum to compare with. Print out the continuous sub-array of values that are equal to sum or the message ‘No sum found’. There...
Explain the sliding window flow protocol and discuss the advantages of this protocol.
Explain the sliding window flow protocol and discuss the advantages of this protocol.
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Project: Given an array numbers. We define a running sum of an array as runningSum[i] =...
Project: Given an array numbers. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of numbers. Example: Input: nums = [1,2,3,4] Output: [1,3,6,10] Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]. You need to do: Create a class called RunningSumArray to perform the main method. Create a class called ArrayOperationto hold the actions for array. In this project, you will need 4methods: 1. Public static Int[] getArray() --- inside ArrayOperationclass,...
Consider this prime sieve in which an array of numbers of size N is created (i.e....
Consider this prime sieve in which an array of numbers of size N is created (i.e. int nums[10] = {2,3,4,5,6,7,8,9,10,11};) and initialized to contain counting numbers starting with 2. The sieve works by taking each number in the array, and “crossing out” (perhaps via a similar array of Booleans) multiples of that number. So beginning with 2, we would “cross out” 4, 6, 8, 10. Then repeat the process for 3, and so on. Numbers that are already “crossed out”...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT