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;
}
0 Comments