DEV Community 👩‍💻👨‍💻

Thivyaa Mohan
Thivyaa Mohan

Posted on

Maximum Subarray - Leetcode Solution

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

A subarray is a contiguous part of an array.

We need to use Kadane's Algorithm here

//Kadane's Algorithm

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int ans =INT_MIN,sum =0;
        int n=nums.size();
        for(int i=0;i<n;++i){
           sum+=nums[i];
           ans = max(ans,sum);
        if(sum<0)
            sum=0;

        }
        return ans;

        }      

};
Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)

Timeless DEV post...

How to write a kickass README

Arguably the single most important piece of documentation for any open source project is the README. A good README not only informs people what the project does and who it is for but also how they use and contribute to it.

If you write a README without sufficient explanation of what your project does or how people can use it then it pretty much defeats the purpose of being open source as other developers are less likely to engage with or contribute towards it.