Question:
How to return the sum of subarray with maximum sum

Summary

Let's assume:

Array- Arr[]

Integer- N

Function: maxSubarraySum()


Solution




class Solution{


    // arr: input array

    // n: size of array

    //Function to find the sum of contiguous subarray with maximum sum.

    long maxSubarraySum(int arr[], int n){

        

        // Your code here

        long currentSum = 0;

        long maxSum = Long.MIN_VALUE;

        

        for(int index = 0; index < n; index++) {

            currentSum += arr[index];

            maxSum = Math.max(currentSum, maxSum);

            if(currentSum < 0) currentSum = 0;

        }

        

        return maxSum;

        

    }

    

}


Suggested blogs:

>Build a minimal ASP.Net Core API with Android Studio for Mac

>Build an Electron application from scratch

>Build Progressive Web Apps with Angular

>Building a web application with Angular and Firebase

>Building Web API using ASP.NET (C#)

>Built Web API using ASP.NET (C#)


Nisha Patel

Nisha Patel

Submit
0 Answers