This file contains 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.Arrays; | |
public class CountDigit { | |
public static final long[] ARRAY = { | |
9L, | |
99L, | |
999L, | |
9999L, | |
99999L, | |
999999L, | |
9999999L, |
This file contains 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 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); |
This file contains 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.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; |
This file contains 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.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; |
This file contains 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.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) { |
This file contains 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.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; |
This file contains 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.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 |
This file contains 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
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 |
This file contains 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 int countNumberofOneBit(int number){ | |
int count=0; | |
while (number!=0){ | |
count++; | |
number=(number-1)&number; | |
} | |
return count; | |
} | |
public static int countNumberofNonLeadingZeroBit(int number){ |
This file contains 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 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) |
OlderNewer