Question

We are given an array A of size n containing n positive and negative integers (the...

We are given an array A of size n containing n positive and negative integers (the array is indexed starting from 0).

Our goal is to find two indices i and j such that 0 ≤ i ≤ j ≤ n and Pk=j k=i A[k] is maximized.

Input sequence: [2, -4, 1, 9, -6, 7, -3]

Output: [1, 9, -6, 7] or i = 2 and j = 5

Input sequence: [1, 2, 3, 4, 5, 6, -3]

Output: [1, 2, 3, 4, 5, 6] or i = 0 and j = 5 Y

ou do not have to output the resulting subarray, just the two indices i and j.

Design an O(n 3 )-time iterative algorithm to solve the problem. Give pseudo-code and a short description. Design an O(n 2 )-time iterative algorithm to solve the problem. Give pseudo-code and a short description.

Homework Answers

Answer #1

(1): O(n3) algorithm for above problem is as follows:

function max_sub_array(lst):
   len=lst.length
   max_sum=0
   max_sum_i=0
   max_sum_j=0
   for i=0 to len-1:
       for j=i to len-1:
           sum=0
           for k=i to j:
               sum+=lst[k]
           if sum>max_sum:
               max_sum=sum
               max_sum_i=i
               max_sum_j=j
   return (max_sum_i,max_sum_j)

Explanation:

Here, we traverse all subarrays which takes O(n2) time and inside it, we add sum of all elements of subarray which takes O(n) in worst case. So time complexity of this algorithm is O(n3)

(2): O(n2) algorithm for above problem is as follows:

function max_sub_array(lst):
   len=lst.length
   max_sum=0
   max_sum_i=0
   max_sum_j=0
   for i=0 to len-1:
       sum=0
       for j=i to len-1:
           sum+=arr[j]
           if sum>max_sum:
               max_sum=sum
               max_sum_i=i
               max_sum_j=j
   return (max_sum_i,max_sum_j)

Explanation:

Here, we traverse all subarrays which takes O(n2) time and here we maintain sum of subarray using sum variable. So time complexity of this algorithm is O(n2) in worst case.

Mention in comments if any mistakes or errors are found. Thank you.

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. Design a deterministic algorithm to solve the following problem. input: An array A[1...n] of n...
2. Design a deterministic algorithm to solve the following problem. input: An array A[1...n] of n integers. output: Two different indices i and j such that A[i] = A[j], if such indices exist. Otherwise, return NONE. Your algorithm must take O(n log(n)) time. You must describe your algorithm in plain English (no pseudocode) and you must explain why the running time of your algorithm is O(n log(n)).
In this problem your task is to find a missing number. The input will always consist...
In this problem your task is to find a missing number. The input will always consist of an array of n positive integers such that the difference between every two consecutive numbers is a fixed constant but one integer is missing. See below for two example inputs/outputs: Input sequence: [0, 2, 4, 6, 10] Output: missing number is 8 Input sequence: [1, 4, 7, 13, 16] Output: missing number is 10 Note that in the first example the constant c...
Divide-and-Conquer technique is famous for providing O(n log n) solutions for problems with O(n2) straight forward...
Divide-and-Conquer technique is famous for providing O(n log n) solutions for problems with O(n2) straight forward solutions. One of those problems is the “Maximum Subarray Sum” or “Maximum Value Contiguous Subsequence": Given a sequence of n numbers A[0 ... n-1], give an algorithm for finding a contiguous subsequence A[i ... j], for which the sum of elements in this subsequence is the maximum we can get by choosing different i or j.   For example: the maximum subarray sum for the...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
First, understand the Selection-sort algorithm below: Selection-sort(A: Array [1..n] of numbers) 1       for i=n down to...
First, understand the Selection-sort algorithm below: Selection-sort(A: Array [1..n] of numbers) 1       for i=n down to 2 2                position=i 3                for j=1 to (i–1) 4                          if A[j]>A[position] then position=j 5                if position ≠ i then 6                          temp=A[i] 7                          A[i]=A[position] 8                          A[position]=temp (4 points) If input A=[12,5,11,6,10,7,9,8], what will A be after the 3rd iteration of the outermost for loop of the Selection-sort algorithm completes? A=[__, __, __, __, __, __, __, __] (8 points) Modify the algorithm to solve the...
Q2) (1 point) Given two lists, L1 of length n and L2 of length m. We...
Q2) (1 point) Given two lists, L1 of length n and L2 of length m. We say that L2 is a subsequence of L1 if we can remove elements from L1 to produce L2. This means that there exists indices i1 < ... < im such that L1[ij] = L2[j] for each j. Design an algorithm that detects if L2 is a subsequence of L1 and outputs the indices i1,....,im if L2 is a subsequence of L1. a.Provide the pseudo-code...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar questions. Please post in a format that can be directly copied. Reasoning on answers would be most helpful but not required. Thank you in advance for your help. 1.Design an algorithm to find all the common elements in two sorted lists of numbers. For example, for the lists 2, 5, 5, 5 and 2, 2, 3, 5, 5, 7, the output should be 2,...
Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0, and returns...
Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0, and returns the total number of 1’s in n’s binary representation. Note that the input is n, not its binary representation. For example, A(9) should return 2 as 9’s binary representation is 1001, while A(7) should return 3 since 7 is 111 in binary. Note that your algorithm should have a running time of O(log n). Justify your answer. You need to do the following: (1)...
Answer the following questions: 1. Given the code segments below with n as the problem size,...
Answer the following questions: 1. Given the code segments below with n as the problem size, answer the following questions: //Code Segment 1 (Consider n as a power of 3) int sum = 0; for(int i = 1; i <= n; i = i*3) sum++;​​​​​​​// statement1 //Code Segment 2: (Consider both possibilities for x) if(x < 5){ for(int i = 1; i <= n; i++)    for(int k = 1; k <= i; k++)    sum++;​​​​​// statement2 } else{ for(int...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar questions. Please post in a format that can be directly copied. Reasoning on answers would be most helpful but not required. Thank you in advance for your help. 2. Consider the following algorithm for finding the distance between the two closest elements in an array of numbers. ALGORITHM MinDistance(A[0..n − 1])//Input: Array A[0..n − 1] of numbers //Output: Minimum distance between two of its...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT