-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem-3.java
46 lines (34 loc) · 864 Bytes
/
Problem-3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* @author AkashGoyal
* @date 27/05/2021
*/
/**
--------------------- Problem----------->> Kadane's Algorithm
Problem Link :- https://practice.geeksforgeeks.org/problems/kadanes-algorithm-1587115620/1
*/
class Solution{
// arr: input array
// n: size of array
//Function to find the sum of contiguous subarray with maximum sum.
int maxSubarraySum(int arr[], int n){
// Your code here
int global=arr[0];
int local=arr[0];
for(int i=1;i<n;i++)
{
if(local+arr[i]>arr[i])
{
local=local+arr[i];
}
else
{
local=arr[i];
}
if(global<local)
{
global=local;
}
}
return global;
}
}