Skip to content

Instantly share code, notes, and snippets.

@deyindra
deyindra / CountDigit
Created November 21, 2014 00:57
Count of Digit in a 32 bit integer
import java.util.Arrays;
public class CountDigit {
public static final long[] ARRAY = {
9L,
99L,
999L,
9999L,
99999L,
999999L,
9999999L,
@deyindra
deyindra / FindCountByBinSearch
Created November 21, 2014 01:02
FindCountByBinSearch
public class FindCountByBinSearch {
//O(logN) complexity
public static <T> int findCountFirst(T[] array, Predicate<T> predicate){
int count=0;
if(array!=null && array.length!=0){
long start =0;
long end = array.length -1;
while(end>=start){
int middle = (int)((end + start)/2);
@deyindra
deyindra / ArrayIterator
Last active August 29, 2015 14:10
ArrayIterator
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ArrayIterator<T> implements Iterator<T>, Iterable<T> {
private T[] array;
private int current;
public ArrayIterator(T[] array) {
this.array = array;
@deyindra
deyindra / TwoDimensionalArrayIterator
Created November 30, 2014 19:11
TwoDimensionalArrayIterator
import java.util.Iterator;
import java.util.NoSuchElementException;
public class TwoDimensionalArrayIterator<T> implements Iterator<T>, Iterable<T> {
private T[][] array;
private int currentRow;
private int currentColumn;
private T obj;
private boolean hasNext;
@deyindra
deyindra / ArrayBasedListIterator
Created December 1, 2014 16:05
ArrayBasedListIterator
import java.util.ListIterator;
import java.util.NoSuchElementException;
public class ArrayBasedListIterator<T> implements ListIterator<T> {
private T[] array;
private int current;
private int prevIndex;
private int nextIndex;
public ArrayBasedListIterator(T[] array) {
@deyindra
deyindra / FilteredIterator
Created December 1, 2014 16:07
FilteredIterator
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class FilteredIterator<T> implements Iterator<T>, Iterable<T> {
private Iterator<T> iterator;
private boolean hasNext;
private Predicate<T> predicate;
private T obj;
@deyindra
deyindra / MergeIterator
Created December 1, 2014 16:10
MergeIterator
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;
import java.util.Queue;
/**Purpose of this class is to merge multiple sorted collection in to a single sorted collection. This used k way merge
* algorithm. Over all complexity is O(nk*logk) where n is number of element in each array and k is number of array
* @author indranil dey
* @param <T> Any generic type which extends {@link java.util.Comparator} interface
* @see java.util.Iterator
@deyindra
deyindra / CountUnival
Last active January 15, 2016 16:47
Unival
package org.idey.algo.datastructure.tree;
public class CountUniValTree<E> {
private TreeNode<E> root;
public CountUniValTree(TreeNode<E> root) {
this.root = root;
}
private boolean countSingleRec(TreeNode<E> root, Counter c) {
// Return false to indicate NULL
@deyindra
deyindra / BinaryPalinDrome
Last active January 17, 2016 20:56
Check if the Binary Representation of a number is Palindrome or not
public static int countNumberofOneBit(int number){
int count=0;
while (number!=0){
count++;
number=(number-1)&number;
}
return count;
}
public static int countNumberofNonLeadingZeroBit(int number){
@deyindra
deyindra / SumNumbersDivisibleBy3Or5
Created January 18, 2016 04:21
Given a number n where n>0 calculate sum of all number which are divisible by 3 or 5 between 1 to n
public static long calculateAP(long start, long totalNumber, long difference){
return (totalNumber*((2*start)+(totalNumber-1)*difference))/2;
}
//Time complexity O(1) and Space complexity O(1)
public static long sumOfDivisbleOf30r5(int number){
long totalNumberDivisibleby3=number/3;
long totalNumberDivisibleby5 = number/5;
long totalNumberDivisibleby15 = number/(3*5);
return calculateAP(3,totalNumberDivisibleby3,3)