Page 1 of 1

Q3. Given an array of n integers and a target value V, design an algorithm to find the maximum length of a subarray such

Posted: Fri Apr 29, 2022 6:59 am
by answerhappygod
Q3 Given An Array Of N Integers And A Target Value V Design An Algorithm To Find The Maximum Length Of A Subarray Such 1
Q3 Given An Array Of N Integers And A Target Value V Design An Algorithm To Find The Maximum Length Of A Subarray Such 1 (193 KiB) Viewed 30 times
Q3. Given an array of n integers and a target value V, design an algorithm to find the maximum length of a subarray such that the sum of the integers stored in the subarray is equal to V, or return 0 if no such subarray exists. Your algorithm should have an expected (or average-case) time complexity of O(n). (20 marks) Examples of input and output of a correct algorithm for this problem are given as follows. Example 1: Input: array[] = {1,-1,5, -2,3}; target value V = 3; Output: 4 Since the longest subarray whose contents sum up to 3 is {1,-1, 5,-2). Example 2: Input: array[] = {10,-3,3,2,-9, 7, 11,25}; target value V = 0; Output: 5 Since the longest subarray whose contents sum up to 0 is {-3, 3, 2, -9,7}.