Data Structure Multiple Choice Questions on “Kadane’s Algorithm”.
1. Kadane’s algorithm is used to find ____________ Answer: c 2. Kadane’s algorithm uses which of the following techniques? Answer: b 3. For which of the following inputs would Kadane’s algorithm produce the INCORRECT output? Answer: d 4. For which of the following inputs would Kadane’s algorithm produce a WRONG output? Answer: b 5. Complete the following code for Kadane’s algorithm: a) max_num(sum, sum + arr[idx]) 6. What is the time complexity of Kadane’s algorithm? Answer: b 7. What is the space complexity of Kadane’s algorithm? Answer: a 8. What is the output of the following implementation of Kadane’s algorithm? a) 6 Answer: d 9. What is the output of the following implementation of Kadane’s algorithm? a) 1 Answer: d 10. Consider the following implementation of Kadane’s algorithm: What changes should be made to the Kadane’s algorithm so that it produces the right output even when all array elements are negative? a) Only Change 1 is sufficient Answer: c
a) Longest increasing subsequence
b) Longest palindrome subsequence
c) Maximum sub-array sum
d) Longest decreasing subsequence
Clarification: Kadane’s algorithm is used to find the maximum sub-array sum for a given array.
a) Divide and conquer
b) Dynamic programming
c) Recursion
d) Greedy algorithm
Clarification: Kadane’s algorithm uses dynamic programming.
a) {0,1,2,3}
b) {-1,0,1}
c) {-1,-2,-3,0}
d) {-4,-3,-2,-1}
Clarification: Kadane’s algorithm works if the input array contains at least one non-negative element. Every element in the array {-4,-3,-2,-1} is negative. Hence Kadane’s algorithm won’t work.
a) {1,0,-1}
b) {-1,-2,-3}
c) {1,2,3}
d) {0,0,0}
Clarification: Kadane’s algorithm doesn’t work for all negative numbers. So, the answer is {-1,-2,-3}.#include
b) sum
c) sum + arr[idx]
d) max_num(sum,ans)
Answer: d
Clarification: The maximum of sum and ans, is stored in ans. So, the answer is max_num(sum, ans).
a) O(1)
b) O(n)
c) O(n2)
d) O(5)
Clarification: The time complexity of Kadane’s algorithm is O(n) because there is only one for loop which scans the entire array exactly once.
a) O(1)
b) O(n)
c) O(n2)
d) None of the mentioned
Clarification: Kadane’s algorithm uses a constant space. So, the space complexity is O(1).#include
b) 7
c) 8
d) 9
Clarification: Kadane’s algorithm produces the maximum sub-array sum, which is equal to 9.#include
b) -1
c) -2
d) 0
Clarification: Kadane’s algorithm produces a wrong output when all the elements are negative. The output produced is 0.1. #include
Change 1 = Line 10: int sum = arr[0], ans = arr[0]
Change 2 = Line 13: sum = max_num(arr[idx],sum+arr[idx])
b) Only Change 2 is sufficient
c) Both Change 1 and Change 2 are necessary
d) No change is required
Clarification: Both change 1 and change 2 should be made to Kadane’s algorithm so that it produces the right output even when all the array elements are negative.