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.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class KthElement { | |
private static <T extends Comparable<T>> T getMedian(List<T> list){ | |
Collections.sort(list); |
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
private static <T> boolean isSafe( | |
final T array[][], | |
final int rowIndex, | |
final int colIndex, | |
final boolean visited[][], | |
final Predicate<T> predicate){ | |
final int ROW = array.length; | |
final int COL = array[0].length; |
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 abstract class AbstractTreeIterator<E> implements Iterator<E> { | |
protected AbstractTreeIterator(TreeNode<E> root){ | |
assert(root!=null); | |
} | |
@Override | |
public void remove() { | |
throw new UnsupportedOperationException("remove is not supported"); | |
} |
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
/** Tree Node class **/ | |
public class TreeNode<E> { | |
private E data; | |
private TreeNode<E> left; | |
private TreeNode<E> right; | |
public TreeNode(E data) { | |
this.data = data; | |
} |
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 <T> boolean instanceOf(T object, Class<?> clazz){ | |
if(object==null){ | |
return false; | |
}else{ | |
Class<?> baseClass = object.getClass(); | |
if (baseClass.isArray() && clazz.isArray()) { | |
return instanceOf(baseClass.getComponentType(), clazz.getComponentType()); | |
} else | |
return !(!baseClass.isArray() && clazz.isArray()) | |
&& !(baseClass.isArray() && !clazz.isArray()) |
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 CustomList<T> { | |
private Map<T, Value<T>> map = new HashMap<T, Value<T>>(); | |
private List<Value<T>> list = new ArrayList<>(); | |
private static final Random RANDOM = new Random(); | |
public boolean add(T object){ | |
Value<T> value = map.get(object); | |
//This mean key does not exists | |
if(value==null){ | |
value = new Value<>(object,list.size()); |
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
//Class Represent NArrayTreeNode | |
public class NArrayTreeNode<E> { | |
private E data; | |
private List<NArrayTreeNode<E>> childs = new ArrayList<>(); | |
public NArrayTreeNode(E data) { | |
this.data = data; | |
} | |
public NArrayTreeNode<E> addChild(NArrayTreeNode<E> child){ |
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.io.Closeable; | |
import java.util.Iterator; | |
/** | |
* Iterator which used some resource needs to be closed after execution | |
* <em>Any concrete {@link ClosableResourceIterator} class either must be initialized with | |
* try with resource or should call {@link ClosableResourceIterator#close()} after complele | |
* execution.</em> | |
* @see Iterator | |
* @see Closeable |
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 boolean isPalinDrome(String str){ | |
if(str==null || str.length()==0){ | |
throw new IllegalArgumentException("Invalid words"); | |
}else{ | |
str = str.trim(); | |
char[] array = str.toCharArray(); | |
Set<Character> sets = new HashSet<>(); | |
for(char ch:array){ | |
boolean add = sets.add(ch); | |
if(!add){ |
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
//O(log(N)) complexity | |
public <T extends Comparable<T>> static in findPivote(T[] array){ | |
int low = 0; | |
int hight = array.length - 1; | |
while(array[low].compareTo(array[high])>0){ | |
int mid = low + (high -low)/2; | |
if(array[mid].compareTo(array[high])>0){ | |
low = mid + 1; | |
}else{ | |
high = mid; |