Skip to content

Commit e5abaca

Browse files
author
harshbhardwaj
committed
LeetCode - DP - MaxContigiousSum
1 parent 38f40f4 commit e5abaca

File tree

6 files changed

+48
-5
lines changed

6 files changed

+48
-5
lines changed

Leetcode/src/com/practise/MajorityElement.java renamed to Leetcode/src/com/practise/array_manupulation/MajorityElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121

2222

23-
package com.practise;
23+
package com.practise.array_manupulation;
2424

2525
public class MajorityElement {
2626

Leetcode/src/com/practise/MedianNumber.java renamed to Leetcode/src/com/practise/array_manupulation/MedianNumber.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.practise;
1+
package com.practise.array_manupulation;
22

33
public class MedianNumber {
44

Leetcode/src/com/practise/MissingNumbers.java renamed to Leetcode/src/com/practise/array_manupulation/MissingNumbers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.practise;
1+
package com.practise.array_manupulation;
22
/*
33
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
44

Leetcode/src/com/practise/TwoSum.java renamed to Leetcode/src/com/practise/array_manupulation/TwoSum.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.practise;
1+
package com.practise.array_manupulation;
22

33
import java.util.HashMap;
44
import java.util.Map;

Leetcode/src/com/practise/FindPalindromes.java renamed to Leetcode/src/com/practise/dynamic_programming/FindPalindromes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.practise;
1+
package com.practise.dynamic_programming;
22

33
import java.util.HashSet;
44
import java.util.Set;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.practise.dynamic_programming;
2+
3+
/*
4+
Kadane Algorithm
5+
Initialize:
6+
max_so_far = 0
7+
max_ending_here = 0
8+
9+
Loop for each element of the array
10+
(a) max_ending_here = max_ending_here + a[i]
11+
(b) if(max_so_far < max_ending_here)
12+
max_so_far = max_ending_here
13+
(c) if(max_ending_here < 0)
14+
max_ending_here = 0
15+
return max_so_far
16+
17+
*/
18+
19+
public class MaxContigiousSum {
20+
21+
public static int findSum(int[] arr) {
22+
23+
int max_so_far = arr[0];
24+
int max_ending = 0;
25+
for (int j : arr) {
26+
max_ending = max_ending + j;
27+
if (max_ending < 0) {
28+
max_ending = 0;
29+
} else if (max_ending > max_so_far) {
30+
max_so_far = max_ending;
31+
}
32+
}
33+
return max_so_far;
34+
}
35+
36+
public static void main(String[] args) {
37+
int[] arr1 = {2, -3, 4, -1, -2, 1, 5, -3};
38+
int[] arr2 = {1 ,2 ,3 ,-2, 5};
39+
System.out.println(MaxContigiousSum.findSum(arr1));
40+
System.out.println(MaxContigiousSum.findSum(arr2));
41+
}
42+
}
43+

0 commit comments

Comments
 (0)