MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / maxSubArray

Method maxSubArray

C++/maximum-subarray.cpp:9–29  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7class Solution {
8public:
9 int maxSubArray(vector<int>& nums) {
10
11 int cs=0;
12 int ms=INT_MIN;
13 // if size of array is 1 return that elemnt .
14 if(nums.size()==1){
15 return nums[0];
16 }
17 //Linearly traverse the array and add to the current sum variable and take maximum .
18 for (int i = 0; i < nums.size(); i++) {
19 cs=cs+nums[i];
20
21
22 ms=max(cs,ms);
23 if(cs<0){
24 cs=0;
25 }
26 }
27
28 return ms;
29}
30
31
32

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected