Question

In: Computer Science

Let S = (s1, s2, . . . , sn) be a given sequence of integer...

Let S = (s1, s2, . . . , sn) be a given sequence of integer numbers. The numbers can be positive or negative. We define a slice of S as a sub- sequence (si,si+1,...,sj) where 1 ≤ i < j ≤ n. The weight of a slice is defined as the sum of its elements. Provide efficient algorithms to answer each of the following questions:

a)Is there any slice with zero weight ?
b)Find the maximum weight slice in the sequence.

Solutions

Expert Solution

a) Is there any slice with zero weight ?

Time Complexity : O(N).

b) Find the maximum weight slice in the sequence.

Algorithm is self explanatory with code :

    int max_sum = 0;
    int current_sum = 0;
    for(int i = 0; i < N; i++)
    {
        current_sum += S[i];
        if(current_sum > max_sum)
            max_sum = current_sum;
        if(current_sum < 0)
            current_sum = 0;
    }
    cout<<max_sum<<"\n";

Time Complexity : O(N).

Like, if this helped :)


Related Solutions

Answer the following Discrete Structures Suppose string s = (s1 s2 ..sn). Give a recursive definition...
Answer the following Discrete Structures Suppose string s = (s1 s2 ..sn). Give a recursive definition of the function numOnes(n), which counts the number of 1s in bit-string of length n, Make sure to define the function for the base case, numOnes(0).
Let (sn) be a sequence that converges. (a) Show that if sn ≥ a for all...
Let (sn) be a sequence that converges. (a) Show that if sn ≥ a for all but finitely many n, then lim sn ≥ a. (b) Show that if sn ≤ b for all but finitely many n, then lim sn ≤ b. (c) Conclude that if all but finitely many sn belong to [a,b], then lim sn belongs to [a, b].
Let S = {s1, s2, s3, s4, s5, s6} be the sample space associated with an...
Let S = {s1, s2, s3, s4, s5, s6} be the sample space associated with an experiment having the probability distribution shown in the accompanying table. If A = {s1, s2} and B = {s1, s5, s6}, find the following. Outcome Probability s1 1 3 s2 1 7 s3 1 6 s4 1 6 s5 1 21 s6 1 7 (a) P(A) = P(B) = (b) P(AC) = P(BC) = (c) P(A ∩ B) = (d) P(A ∪ B) =...
If there are two energy states, S1 and S2 respectively such that S2>S1 then there exists...
If there are two energy states, S1 and S2 respectively such that S2>S1 then there exists some probability for an atom in S2 to decay to S1. What actually causes the atom to decay to the lower energy state? is it the fact that the lower state is more probable for the atom to be in as given by the Boltzmann Factor? so since it is more probable, it has more microstates and entropy causes it to decay? Please help...
Let S1 and S2 be any two equivalence relations on some set A, where A ≠...
Let S1 and S2 be any two equivalence relations on some set A, where A ≠ ∅. Recall that S1 and S2 are each a subset of A×A. Prove or disprove (all three): The relation S defined by S=S1∪S2 is (a) reflexive (b) symmetric (c) transitive
Let sn be a Cauchy sequence such that ∀n > 1, n ∈ N, ∃m >...
Let sn be a Cauchy sequence such that ∀n > 1, n ∈ N, ∃m > 1, m ∈ N such that |sn − m| = 1/3 (this says that every term of the sequence is an integer plus or minus 1/3 ). Show that the sequence sn is eventually constant, i.e. after a point all terms of the sequence are the same
Write each vector as a linear combination of the vectors in S. (Use s1 and s2,...
Write each vector as a linear combination of the vectors in S. (Use s1 and s2, respectively, for the vectors in the set. If not possible, enter IMPOSSIBLE.) S = {(1, 2, −2), (2, −1, 1)} (a)    z = (−8, −1, 1) z = (b)    v = (−2, −5, 5) v = (c)    w = (1, −23, 23) w = (d)    u = (2, −6, −6) u =
Write each vector as a linear combination of the vectors in S. (Use s1 and s2,...
Write each vector as a linear combination of the vectors in S. (Use s1 and s2, respectively, for the vectors in the set. If not possible, enter IMPOSSIBLE.) S = {(1, 2, −2), (2, −1, 1)} (a)    z = (−13, −1, 1) z =      (b)    v = (−1, −5, 5) v =      (c)    w = (−2, −14, 14) w =      (d)    u = (1, −4, −4) u =     
Suppose you have a set of real-valued waveforms {s1(t), s2(t),..., sN(t)}, and you want to find...
Suppose you have a set of real-valued waveforms {s1(t), s2(t),..., sN(t)}, and you want to find a basis for the span of their complex envelopes. The obvious approach would be to first downconvert each of the waveforms, and then apply the Gram-Schmidt procedure to the set of complex envelopes. Will we get the same answer if we first apply Gram-Schmidt, and then downconvert? Justify your answer.
def anagramSolution1(s1,s2): alist = list(s2) pos1 = 0 stillOK = True while pos1 < len(s1) and...
def anagramSolution1(s1,s2): alist = list(s2) pos1 = 0 stillOK = True while pos1 < len(s1) and stillOK: pos2 = 0 found = False while pos2 < len(alist) and not found: if s1[pos1] == alist[pos2]: found = True else: pos2 = pos2 + 1 if found: alist[pos2] = None else: stillOK = False pos1 = pos1 + 1 return stillOK include all operations and calculate the Big-O and the values for c and n0.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT