Data Structure Multiple Choice Questions on “Maximum Sum of Continuous Subarray – 1”.
1. Given a one-dimensional array of integers, you have to find a sub-array with maximum sum. This is the maximum sub-array sum problem. Which of these methods can be used to solve the problem? Answer: d 2. Find the maximum sub-array sum for the given elements. Answer: b 3. Find the maximum sub-array sum for the given elements. Answer: d 4. Consider the following naive method to find the maximum sub-array sum: Which line should be inserted to complete the above code? Answer: d 5. What is the time complexity of the following naive method used to find the maximum sub-array sum in an array containing n elements? a) O(n2) Answer: a 6. What is the space complexity of the following naive method used to find the maximum sub-array sum in an array containing n elements? a) O(n2) Answer: b 7. What is the output of the following naive method used to find the maximum sub-array sum? a) 6 8. What is the time complexity of the divide and conquer algorithm used to find the maximum sub-array sum? 9. What is the space complexity of the divide and conquer algorithm used to find the maximum sub-array sum?
a) Dynamic programming
b) Two for loops (naive method)
c) Divide and conquer
d) Dynamic programming, naïve method and Divide and conquer methods
Clarification: Dynamic programming, naïve method and Divide and conquer methods can be used to solve the maximum sub array sum problem.
{2, -1, 3, -4, 1, -2, -1, 5, -4}
a) 3
b) 5
c) 8
d) 6
View Answer
Clarification: The maximum sub-array sum is 5.
{-2, -1, -3, -4, -1, -2, -1, -5, -4}
a) -3
b) 5
c) 3
d) -1
Clarification: All the elements are negative. So, the maximum sub-array sum will be equal to the largest element. The largest element is -1 and therefore, the maximum sub-array sum is -1.#include
a) tmp_max = cur_max
b) break
c) continue
d) cur_max = tmp_max
Clarification: If the tmp_max element is greater than the cur_max element, we make cur_max equal to tmp_max, i.e. cur_max = tmp_max.#include
b) O(n)
c) O(n3)
d) O(1)
Clarification: The naive method uses two for loops. The outer loop runs from 0 to n,
i.e. i = 0 : n.
The inner loop runs from i to n, i.e. j = i : n.
Therefore, the inner loop runs:
n times when the outer loop runs the first time.
(n-1) times when the outer loop runs the second time.
:
:
:
2 times when the outer loop runs (n-1)th time.
1 time when the outer loop runs nth time.
Therefore, time complexity = n + (n-1) + (n-2) + …… + 2 + 1 = n * (n + 1) /2 = O(n2).#include
b) O(1)
c) O(n3)
d) O(n)
Clarification: The naive method uses only a constant space. So, the space complexity is O(1).#include
b) 9
c) 7
d) 4
Answer: c
Clarification: The naive method prints the maximum sub-array sum, which is 7.
a) O(n)
b) O(logn)
c) O(nlogn)
d) O(n2)
Answer: c
Clarification: The time complexity of the divide and conquer algorithm used to find the maximum sub-array sum is O(nlogn).
a) O(n)
b) O(1)
c) O(n!)
d) O(n2)
Answer: b
Clarification: The divide and conquer algorithm uses a constant space. So, the space complexity is O(1).