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
//smallest element greater than or equal to key | |
public static <T extends Comparable<T>> int findCeil(final T[] array, final T key){ | |
int low=0; | |
int high = array.length-1; | |
while (low<high){ | |
int mid = low + (high-low)/2; | |
if(key.compareTo(array[mid])>0){ | |
low = mid+1; | |
}else{ | |
high = 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
public int findOddOne(int[] array){ | |
int xor = array[0]; | |
for(int i=1;i<array.length;i++){ | |
xor = xor ^ array[i]; | |
} | |
return xor; | |
} |
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 Set<Integer> findOddMultiple(int[] array){ | |
Set<Integer> oddSet = new HashSet<>(); | |
for(int val:array){ | |
boolean status = oddSet.add(val); | |
if(!status){ | |
oddSet.remove(val); | |
} | |
} | |
return oddSet; | |
} |
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 interface Predicate<T>{ | |
boolean process(T object); | |
} | |
public static <T> int findCount(T[][] array, Predicate<T> predicate){ | |
int count=0; | |
int row=0; | |
int col = array[0].length-1; | |
while (row<=array.length-1 && col>=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
import java.util.PriorityQueue; | |
import java.util.function.Function; | |
public abstract class AggregateFunction<T extends Comparable<T>> implements Function<PriorityQueue<TimeSeriesType<T>>, T> { | |
@Override | |
public <V> Function<PriorityQueue<TimeSeriesType<T>>, V> andThen(Function<? super T, ? extends V> after) { | |
throw new UnsupportedOperationException("Not supported..."); | |
} | |
@Override | |
public <V> Function<V, T> compose(Function<? super V, ? extends PriorityQueue<TimeSeriesType<T>>> before) { |
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 class TreeNode<E> { | |
private E data; | |
private TreeNode<E> left; | |
private TreeNode<E> right; | |
public TreeNode(E data) { | |
this.data = data; | |
} | |
public TreeNode<E> addLeft(TreeNode<E> left){ |
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
import java.util.Collections; | |
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
import java.util.PriorityQueue; | |
public class MedianIterator<T extends Comparable<T>> implements Iterator<T>{ | |
private int size; | |
private T[] array; | |
private T median; | |
private int currentIndex; |
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 void generatePermutation(String str, final int index, final String prefix, List<String> list, | |
final char[] allChars, final char replaceAbleChar){ | |
if(str.equals("")){ | |
list.add(prefix); | |
}else{ | |
char[] array = str.toCharArray(); | |
if(array[index]!=replaceAbleChar){ | |
generatePermutation(str.substring(index+1),0,prefix+array[index],list,allChars,replaceAbleChar); | |
}else{ |
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 List<String> wordBreak(String str){ | |
if(str==null){ | |
throw new IllegalArgumentException("Invalid String"); | |
} | |
List<String> wordList = new ArrayList<>(); | |
String subString=""; | |
boolean isQuoteFound=false; | |
for(char c:str.toCharArray()){ | |
if(c!=' ' && c!='"'){ |