Skip to content

Instantly share code, notes, and snippets.

View MithraTalluri's full-sized avatar

Mithra Talluri MithraTalluri

View GitHub Profile
//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]);
}
c - j = j' - c
So, mirror index of j:
j' = (2 * c) - j
@MithraTalluri
MithraTalluri / KadanesAlgorithmSnippet3.java
Last active April 23, 2019 11:14
Maximum Subarray Sum with indices on 1D array (Kadane's Algorithm)
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;
@MithraTalluri
MithraTalluri / KadanesAlgorithmSnippet1.java
Created February 20, 2019 07:48
Maximum Subarray Sum on 1D array (Kadane's Algorithm)
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];
@MithraTalluri
MithraTalluri / KadanesAlgorithmSnippet2.java
Last active April 23, 2019 11:14
Maximum Subarray Sum on 1D array (Kadane's Algorithm)
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;
}
@MithraTalluri
MithraTalluri / BinarySearchConditions.csv
Last active March 7, 2022 02:20
Conditions for Binary Search
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)
@MithraTalluri
MithraTalluri / BinarySearchExample2.java
Created December 25, 2018 08:04
Min Element In Array >= Key (Binary Search)
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];
}
@MithraTalluri
MithraTalluri / BinarySearchExample1.java
Created December 25, 2018 06:06
Max Element In Array <= Key (Binary Search)
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];
}
@MithraTalluri
MithraTalluri / BinarySearchMidCalculation.csv
Created December 24, 2018 08:31
Binary Search mid Calculation
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
@MithraTalluri
MithraTalluri / BinarySearchIterative.java
Created December 24, 2018 07:15
Binary Search (Iterative) in Java
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;
}