Skip to content
Related Articles

Related Articles

Algorithms Quiz | Sudo Placement [1.5] | Question 8

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 16 Aug, 2018

Below is the code to find maximum subarray sum, with errors (which may results to wrong output) in the statement which is commented on right half after code statement. You need to choose the correct option for which corresponding statement is wrong.




#include<iostream>
using namespace std;
   
// Function to calculate maximum subarray sum
int maxSubArraySum(int a[ ], int size)
{
   int max_so_far = a[0];
   int curr_max = a[0];
   
   for (int i = 1; i < size; i++)
   {
        curr_max = max(a[i], curr_max); // statement 1
        max_so_far = max(max_so_far, curr_max); // statement 2
   }
   return max_so_far;
}
   
/* Driver program to test maxSubArraySum */
int main()
{
   int a[] =  {-2, -3, 4, -1, -2, 1, 5, -3};
   int n = sizeof(a)/sizeof(a[0]);
   int max_sum = maxSubArraySum(a, n);
   cout << "Maximum contiguous sum is " << max_sum;
   return 0;
}

(A) Statement 1 and statement 2 both.
(B) Statement 1 only.
(C) Statement 2 only.
(D) Neither statement 1 nor statement 2.


Answer: (B)

Explanation: Refer : https://www.geeksforgeeks.org/largest-sum-contiguous-subarray/

Quiz of this Question

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!