Question: write this in c++ code Longest Balanced Span In the longest balanced span problem, we search for a large sub-vector whose elements sum to zero.


write this in c++ code
Longest Balanced Span In the longest balanced span problem, we search for a large sub-vector whose elements sum to zero. ongest balanced span problem nput: a vector of n integers utput: The span of indices s,e E [0, n) with se such that e- IFS nd the number of elements in that span is maximized. In the event of a tie, return the later span. A balanced span is a sequence of integers that adds up to zero. So 4, 2, -3, -3 is a balanced span because 4+2-3-3--0. Also any sequence of zeroes counts as a balanced span, such as 0, 0, 0. In this problem we search for the longest balanced span, or in other words the span containing the most elements. So if Then a correct algorithm would return the indices 5, 8 because the span at indices 5, 6,7 contains the values -3, 1, 2 which is balanced. V also contains a shorter balanced span -1, 1, but a correct algorithm prefers the 3-element balanced span over the 2-element balanced span. The following algorithm solves the longest balanced span problem. longest_balanced_span(V) best None for s from 0 to n: for e from s+1 to n if the sum of elements in [s] up to but not including Vle] is zero: if best is None or [s. e) contains more elements than best: best[s, e) return best
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
