Skip to content

Commit cf35407

Browse files
committed
added Kadanes's Algorithm
1 parent 542f342 commit cf35407

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

Arrays/KadanesAlgorithm.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Arrays;
2+
3+
public class KadanesAlgorithm {
4+
// maximum subArray or Kadane's Algorithm
5+
public int maxSubArray(int[] nums) {
6+
7+
int maxSum = nums[0];
8+
int currSum = maxSum;
9+
10+
for(int i = 1 ; i < nums.length ; i++) {
11+
12+
if(currSum + nums[i] < nums[i]) {
13+
currSum = nums[i];
14+
}else {
15+
currSum = currSum + nums[i];
16+
}
17+
18+
if(currSum > maxSum) {
19+
maxSum = currSum;
20+
}
21+
}
22+
return maxSum;
23+
}
24+
}

LinkedList/SinglyLinkedList.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,6 @@ static boolean compareLists(ListNode head1, ListNode head2) {
317317
}
318318

319319
public static ListNode removeDuplicates(ListNode head) {
320-
321320
// if list is empty or if list contains a single node
322321
if(head == null || head.next == null){
323322
return head;
@@ -328,14 +327,13 @@ public static ListNode removeDuplicates(ListNode head) {
328327

329328
// if head's next is null, it's the end of list
330329
while(head.next!=null){
331-
// checking head's data with it's next data
330+
// checking head's data with its next data
332331
if(head.data != head.next.data){
333332
head = head.next;
334333
}else{
335334
head.next = head.next.next;
336335
}
337336
}
338-
339337
return current;
340338
}
341339

0 commit comments

Comments
 (0)