Skip to content

Instantly share code, notes, and snippets.

@deyindra
deyindra / PairFromBST
Created August 29, 2017 21:46
TreeNode
//Time O(n) and Space O(longN)
public static List<Pair<Integer, Integer>> getPairs(final TreeNode<Integer> tree, final int sum){
List<Pair<Integer, Integer>> list = new ArrayList<>();
Stack<TreeNode<Integer>> left = new Stack<>();
Stack<TreeNode<Integer>> right = new Stack<>();
TreeNode<Integer> leftCurrent = tree;
TreeNode<Integer> rightCurrent = tree;
while (!left.isEmpty() || !right.isEmpty() || leftCurrent!=null || rightCurrent!=null){
@deyindra
deyindra / FindPair
Last active August 29, 2017 21:46
Find pair from Sorted Array
//Time complexity O(N), Space Complexity O(1)
public static List<Pair<Integer, Integer>> getPairs(int[] array, final int sum){
int start = 0;
int end = array.length-1;
List<Pair<Integer, Integer>> list = new ArrayList<>();
while (start < end){
int resultSum = array[start] + array[end];
if(resultSum < sum){
start++;
}else if (resultSum > sum){
@deyindra
deyindra / NthSmallestOrLargest
Created August 24, 2017 00:30
Get Nth Smallest or Largest Element from BST (Binary Search Tree)
public class TreeNode<E> {
private E data;
private TreeNode<E> left;
private TreeNode<E> right;
public TreeNode(E data) {
this.data = data;
}
public void setLeft(TreeNode<E> left) {
@deyindra
deyindra / Trie
Last active July 12, 2017 02:11
Phone Directory Lookup
package org.idey.algo.datastructure;
import java.util.*;
public class Trie<T> {
private TrieNode<T> root;
public Trie() {
this.root = new TrieNode<>();
}
public void insert(String word, T object){
import java.util.List;
import java.util.Objects;
abstract class AbstractListCommand<T> {
protected final String opName;
protected final int index;
protected T newObject;
protected T oldObject;
protected AbstractListCommand(final int index, final String opName) {
@deyindra
deyindra / FlattentNestedIterator
Created May 15, 2017 21:42
Unfolded Iterator
public class FlattentNestedIterator<T> implements Iterator<T>{
private Stack<Iterator<Nested<T>>> stack = new Stack<>();
private T current;
private boolean hasNext;
public FlattentNestedIterator(Iterator<Nested<T>> nestedIterator) {
if(nestedIterator == null){
throw new IllegalArgumentException("Invalid Iterator");
}
stack.push(nestedIterator);
advance();
@deyindra
deyindra / Planidromic.java
Created April 13, 2017 21:45
Planidromic Number
/**
* Calculate how many palindromic n-digit numbers are present
* for 2 digit the palindromic numbers are 11, 22, 33, 44, 55, 66, 77, 88, 99 (total of 9)
**/
public static int numberOfPalindromicProblem(int n){
if((n & 1) != 0){
n = n/2;
}else{
n = n/2 -1;
}
@deyindra
deyindra / Associative Operation
Created March 27, 2017 16:22
Associative Operation
import java.util.Arrays;
//Space and time complexity O(n)
public abstract class AssociativeOperation {
protected int intVar;
protected AssociativeOperation(int intVar) {
this.intVar = intVar;
}
@deyindra
deyindra / LazyPrime
Created March 13, 2017 17:17
Lazy Prime Stream
object PrimeStream {
def apply(end : Int):Stream[Int] = {
def greater:(Int,Int) =>Boolean = (x,y) => x>=y
def transform: (Int) => Int = (x) => x+1
val intStream:Stream[Int] = Stream(2,end)(greater,transform)
def primeStream(stream:Stream[Int]) : Stream[Int] = {
new ConStream[Int](stream.head, primeStream(Stream(stream.tail)(_%stream.head!=0)))
}
primeStream(intStream)
}
@deyindra
deyindra / Stream
Last active March 13, 2017 17:20
Custom Stream
import scala.annotation.tailrec
/**
* Created by i.dey on 3/12/17.
*/
trait Stream[+T] {
def isEmpty : Boolean
def head : T
def tail : Stream[T]