adplus-dvertising
frame-decoration

Question

Complete the following code for Kadane’s algorithm:
#include<stdio.h>
int max_num(int a, int b)
{ 
      if(a > b)
         return a; 
      return b;
}
int kadane_algo(int *arr, int len)
{	
     int ans, sum, idx;
     ans =0;
     sum =0;
     for(idx =0; idx < len; idx++)
     {
         sum = max_num(0,sum + arr[idx]);
         ans = ___________;
     }
     return ans;
}
int main()
{
     int arr[] = {-2, -3, 4, -1, -2, 1, 5, -3},len=7;
     int ans = kadane_algo(arr,len);
     printf("%d",ans);
     return 0;
}

a.

max_num(sum, sum + arr[idx])

b.

sum

c.

sum + arr[idx].

d.

max_num(sum,ans)

Answer: (d).max_num(sum,ans)

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. Complete the following code for Kadane’s algorithm:

Similar Questions

Discover Related MCQs

Q. What is the time complexity of Kadane’s algorithm?

Q. What is the space complexity of Kadane’s algorithm?

Q. The longest increasing subsequence problem is a problem to find the length of a subsequence from a sequence of array elements such that the subsequence is sorted in increasing order and it’s length is maximum. This problem can be solved using __________

Q. Find the longest increasing subsequence for the given sequence:
{10, -10, 12, 9, 10, 15, 13, 14}

Q. Find the length of the longest increasing subsequence for the given sequence:
{-10, 24, -9, 35, -21, 55, -41, 76, 84}

Q. For any given sequence, there will ALWAYS be a unique increasing subsequence with the longest length.

Q. The number of increasing subsequences with the longest length for the given sequence are:
{10, 9, 8, 7, 6, 5}

Q. In the brute force implementation to find the longest increasing subsequence, all the subsequences of a given sequence are found. All the increasing subsequences are then selected and the length of the longest subsequence is found. What is the time complexity of this brute force implementation?

Q. Given a rod of length n and the selling prices of all pieces smaller than equal to n, find the most beneficial way of cutting the rod into smaller pieces. This problem is called the rod cutting problem. Which of these methods can be used to solve the rod cutting problem?

Q. You are given a rod of length 5 and the prices of each length are as follows:
length price

1 2

2 5

3 6

4 9

5 9

What is the maximum value that you can get after cutting the rod and selling the pieces?

Q. Consider the brute force implementation of the rod cutting problem in which all the possible cuts are found and the maximum value is calculated. What is the time complexity of this brute force implementation?

Q. You are given a rod of length 10 and the following prices.
length price

1 2

2 5

3 6

4 9

5 9

6 17

7 17

8 18

9 20

10 22

Which of these pieces give the maximum price?

Q. For every rod cutting problem there will be a unique set of pieces that give the maximum price.

Q. You are given an array of elements where each array element represents the MAXIMUM number of jumps that can be made in the forward direction from that element. You have to find the minimum number of jumps that are required to reach the end of the array. Which of these methods can be used to solve the problem?

Q. Consider the following array:
{1, 3, 5, 8, 9, 2, 6, 7, 6}

What is the minimum number of jumps required to reach the end of the array?

Q. For a given array, there can be multiple ways to reach the end of the array using minimum number of jumps.

Q. For any array, given that at most one element is non-zero, it is ALWAYS possible to reach the end of the array using minimum jumps.

Q. The Knapsack problem is an example of ____________

Q. Which of the following methods can be used to solve the Knapsack problem?

Q. You are given a knapsack that can carry a maximum weight of 60. There are 4 items with weights {20, 30, 40, 70} and values {70, 80, 90, 200}. What is the maximum value of the items you can carry using the knapsack?