-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.kadanesAlgo.java
59 lines (45 loc) · 1.57 KB
/
13.kadanesAlgo.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
47
48
49
50
51
52
53
54
55
56
57
58
// { Driver Code Starts
import java.io.*;
class Array {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim()); //Inputting the testcases
while(t-->0){
//size of array
int n = Integer.parseInt(br.readLine().trim());
int arr[] = new int[n];
String inputLine[] = br.readLine().trim().split(" ");
//adding elements
for(int i=0; i<n; i++){
arr[i] = Integer.parseInt(inputLine[i]);
}
Kadane obj = new Kadane();
//calling maxSubarraySum() function
System.out.println(obj.maxSubarraySum(arr, n));
}
}
}
// } Driver Code Ends
class Kadane{
// video link : https://www.youtube.com/watch?v=VMtyGnNcdPw
// Function to find subarray with maximum sum
// arr: input array
// n: size of array
int maxSubarraySum(int arr[], int n){
int currentSum = arr[0];
int overAllSum = arr[0];
for(int i=1;i<n;i++){ //i =1 bcz 0 phle hi consider kar liye hai
//here make currensum 2 case +ve and -ve and arr[i] , +ve and -ve on comparing getting below result
if(currentSum>=0)
currentSum = currentSum+arr[i];
else{
currentSum = arr[i];
}
// if currentsum>=overallsum update overallSum;
if(currentSum>=overAllSum){
overAllSum = currentSum;
}
}
return overAllSum;
}
}