-4
  1. Why is she using [n+1] instead of N directly and then that line no. 23 what is that equality?
  2. Why do we use INT_MIN for Maximum numbers or arrays, and `INT_MAX for minimum things?
#include <bits/stdc++.h>
using namespace std;

//Question is of Find the Subarray with Maximum sum//

int main(){
    
    int n;
    cin >> n;

    int arr[n];

    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    
    int currsum[n+1]; //CurrentSum = currsum[]
    currsum[0] = 0;

    for (int i = 1; i <= n; i++)
    {
        currsum[i] = currsum[i-1] + arr[i-1];
    }

    int MaxSum = INT_MIN;

    for (int i = 1; i <= n; i++)
    {
        int sum = 0;
        for (int j = 0; j <i; j++){
            sum = currsum[i] + currsum[j];
            MaxSum = max(sum, MaxSum);
        }
    }
return 0; 
}
UUC110
  • 1
  • 4
  • What do mean "what is that equality"? Line 23 (when I count it) is just a bracket - can you comment your code to show where you mean? – kenntnisse May 09 '22 at 16:45
  • 1
    who is "she" ? When you copy code from somewhere you should provide a reference to the source. Then `#include ` is not standard C++ and `int arr[n];` is not standard C++. [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – 463035818_is_not_an_ai May 09 '22 at 17:50

1 Answers1

1
  1. Have you heard of prefix sums? (aka running totals, cumulative sums, scans). N+1 is used because prefix sums also needs to include an empty subarray.
  2. You use INT_MIN for maximum because the max() function takes the larger of the two values, so MaxSum is always increasing. If you make MaxSum = 0 at the beginning, or some other number, there is a chance that 0 is larger than any of the actual sums. To make sure that the default value does not override the actual values, you set it to as low as possible.
kenntnisse
  • 429
  • 2
  • 12