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 {
    int maxSubArray(vector<int>& nums) {
        int ans =INT_MIN,sum =0;
        int n=nums.size();
        for(int i=0;i<n;++i){
           ans = max(ans,sum);

        return ans;


Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Hey 😍

Want to help the DEV Community feel more like a community?

Head over to the Welcome Thread and greet some new community members!

It only takes a minute of your time, and goes a long way!