This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //see if the mirror of i is expanding beyond the left boundary of current longest palindrome at center c | |
| //if it is, then take r - i | |
| //else take P[mirror] | |
| if(i < r){ | |
| P[i] = Math.min(r - i, P[mirror]); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| c - j = j' - c | |
| So, mirror index of j: | |
| j' = (2 * c) - j |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static int KadaneWithIndices(int[] array){ | |
| int currentMax = 0; | |
| int totalMax = Integer.MIN_VALUE; | |
| int startIndex = 0, endIndex = 0, tempIndex = 0; | |
| for(int i = 0; i < array.length; i++){ | |
| currentMax += array[i]; | |
| if(currentMax < 0){ | |
| currentMax = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public int Kadanes(int[] array){ | |
| int n = array.length; | |
| int[] dp = new int[n]; | |
| //base condition | |
| dp[0] = array[0]; | |
| int answer = Integer.MIN_VALUE; | |
| for(int i = 1; i < n; i++){ | |
| dp[i] = Math.max(dp[i - 1], 0) + array[i]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public int getMaxSubarraySum(int[] array){ | |
| int currentMax = Integer.MIN_VALUE; | |
| int totalMax = Integer.MIN_VALUE; | |
| for(int i = 0; i < array.length; i++){ | |
| currentMax = Math.max(currentMax, 0) + array[i]; | |
| totalMax = Math.max(totalMax, currentMax); | |
| } | |
| return totalMax; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| low | high | mid | while(?) | |
|---|---|---|---|---|
| mid + 1 | mid - 1 | low + ((high - low) / 2) | low <= high | |
| mid + 1 | mid | low + ((high - low) / 2) | low < high | |
| mid | mid - 1 | low + ((high - low + 1) / 2) | low < high | |
| mid | mid | X (infinite loop) | X (invalid) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int binarySearchExample2(int[] array, int key){ | |
| int length = array.length; | |
| int low = 0; | |
| int high = length - 1; | |
| while(low < high){ | |
| int mid = low + ((high - low) / 2); | |
| int current = array[mid]; | |
| if(current == key){ | |
| return array[mid]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int binarySearchExample1(int[] array, int key){ | |
| int length = array.length; | |
| int low = 0; | |
| int high = length - 1; | |
| while(low < high){ | |
| int mid = low + ((high - low + 1) / 2); | |
| int current = array[mid]; | |
| if(current == key){ | |
| return array[mid]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| low | high | (low + high) / 2 | low + ((hi - low) / 2) | |
|---|---|---|---|---|
| 3 | 11 | 7 | 7 | |
| 3 | 10 | 6 | 6 | |
| -11 | -3 | -7 | -7 | |
| -10 | -3 | -6 | -7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| boolean binarySearchIterative(int[] array, int key){ | |
| int length = array.length; | |
| int low = 0; | |
| int high = length - 1; | |
| while(low <= high){ | |
| int mid = (low + high) / 2; | |
| int current = array[mid]; | |
| if(current == key){ | |
| return true; | |
| } |