Question

Let A = (A1, A2, A3,.....Ai) be defined as a sequence containing positive and negative integer...

Let A = (A1, A2, A3,.....Ai) be defined as a sequence containing positive and negative integer numbers.

A substring is defined as (An, An+1,.....Am) where 1 <= n < m <= i.

Now, the weight of the substring is the sum of all its elements.

Showing your algorithms and proper working:

1) Does there exist a substring with no weight or zero weight?
2) Please list the substring which contains the maximum weight found in the sequence.

Homework Answers

Answer #1

Part 1 :

Algorithm :

func (A) :

Let A be an input Array
1.Run loop i from 0 to leangth of Array A
2.Initialize sum=0
3.Run loop j from i to length of Array A
4.Add element at jth index of A to sum
5. If sum ==0 then print "Substring exist" and come out of the function 'func'.
6. End of j loop
7. End of i loop
8. print "No such substring exist"   

Note=> I am adding python code so as to explain the algorithm in a better way and to show outputs as well.

Code and Outputs :

Output 2:

Part 2 :

Algorithm :

func (A) :

Let A be an input Array
1. Initialize max value as first element of Array A, index start as 0 and index end as 0
1.Run loop i from 0 to leangth of Array A
2.Initialize sum=0
3.Run loop j from i to length of Array A
4.Add element at jth index of A to sum
5. If sum is greter tha max value then make max=sum , starting index 'start' = i and last index 'last' =j
6. End of j loop
7. End of i loop
8. print substring starting from index start to index end

Code and Outputs :

Output 2 :

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
2. Exercise 19 section 5.4. Suppose that a1, a2, a3, …. Is a sequence defined as...
2. Exercise 19 section 5.4. Suppose that a1, a2, a3, …. Is a sequence defined as follows: a1=1 ak=2a⌊k/2⌋ for every integer k>=2. Prove that an <= n for each integer n >=1. plzz send with all the step
1. (24 pts.) For which integer values of the constant k does the sequence {a1, a2,...
1. (24 pts.) For which integer values of the constant k does the sequence {a1, a2, a3, . . .} defined by an = (5n^k + 3)/(3n^k + 5) converge? For those values of k, compute the limit of the sequence {a1, a2, a3, . . .}.
1) Suppose a1, a2, a3, ... is a sequence of integers such that a1 =1/16 and...
1) Suppose a1, a2, a3, ... is a sequence of integers such that a1 =1/16 and an = 4an−1. Guess a formula for an and prove that your guess is correct. 2) Show that given 5 integer numbers, you can always find two of the numbers whose difference will be a multiple of 4. 3) Four cats and five mice form a row. In how many ways can they form the row if the mice are always together? Please help...
A sequence is defined by a1=2 and an=3an-1+1. Find the sum a1+a2+⋯+an
A sequence is defined by a1=2 and an=3an-1+1. Find the sum a1+a2+⋯+an
Consider the sequence defined recursively by an+1 = (an + 1)/2 if an is an odd...
Consider the sequence defined recursively by an+1 = (an + 1)/2 if an is an odd number an+1 = an/2 if an is an even number (a) Let a0 be equal to the last digit in your student number, and compute a1, a2, a3, a4. (b) Suppose an = 1, and find an+4. (c) If a0 = 4, does limn→∞ an exist?
Consider the following sequence: 0, 6, 9, 9, 15, 24, . . .. Let the first...
Consider the following sequence: 0, 6, 9, 9, 15, 24, . . .. Let the first term of the sequence, a1 = 0, and the second, a2 = 6, and the third a3 = 9. Once we have defined those, we can define the rest of the sequence recursively. Namely, the n-th term is the sum of the previous term in the sequence and the term in the sequence 3 before it: an = an−1 + an−3. Show using induction...
In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci...
In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … The sequence Fn of Fibonacci numbers is defined by the recurrence relation: Fn = Fn-1 + Fn with seed values F1 = 1 F2 = 1 For more information on...
We are given a sequence of numbers: 1, 3, 5, 7, 9, . . . and...
We are given a sequence of numbers: 1, 3, 5, 7, 9, . . . and want to prove that the closed formula for the sequence is an = 2n – 1.          What would the next number in the sequence be? What is the recursive formula for the sequence? Is the closed formula true for a1? What about a2? What about a3? Critical Thinking How many values would we have to check before we could be sure that the...
{- Alexa loves movies and maintains a list of negative and/or positive integer ratings for the...
{- Alexa loves movies and maintains a list of negative and/or positive integer ratings for the n movies in her collection. She's getting ready for a film festival and wants to choose some subsequence of movies from her collection to bring such that the following conditions are satisfied: The collective sum of their ratings is maximal. She must go through her list in order and cannot skip more than one movie in a row. In other words, she cannot skip...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class Zillion implements a decimal counter that allows numbers with an effectively infinite number of digits. Of course the number of digits isn’t really infinite, since it is bounded by the amount of memory in your computer, but it can be very large. 1. Examples. Here are some examples of how your class Zillion must work. I’ll first create an instance of Zillion. The string...