Find the Maximum sum in array in program c++

Maximum Sum of array 


#include <iostream>
using namespace std;

int maxSubArray(int *nums, int n)
{
    int ans = INT_MIN;
    for (int i = 0; i < n; i++)
    {
        for (int j = i, sum = 0; j < n; j++)
        {
            sum += nums[j];
            ans = max(ans, sum);
        }
    }
    return ans;
}

int main()
{
    int n;
    cout << "Enter the Number of Element " << endl;
    cin >> n;

    int arr[n];
    cout << "Enter the Element of Array " << endl;
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }

    cout << "The Maximum Sum is ";
    cout << maxSubArray(arr, n) << endl;
}



Post a Comment

0 Comments