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].
Consider the sequence sn defined as: s0 = 1 s1 = 1 sn = 2sn-1 +...
Consider the sequence sn defined as: s0 = 1 s1 = 1 sn = 2sn-1 + sn-2 What is the base case for this recursive relation? Find s5 Write the pseudocode for a recursive function to find Sn for any arbitrary value of n. Create a non-recursive formula for finding the nth term in the sequence in O(1) time.
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
Program in C: The strncpy(s1,s2,n) function copies exactly n characters from s2 to s1, truncating s2...
Program in C: The strncpy(s1,s2,n) function copies exactly n characters from s2 to s1, truncating s2 or padding it with extra null characters as necessary. The target string may not be null-terminated if the length of s2 is n or more. The function returns s1. Write your own version of this function. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
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 =     
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT