Find a possible number NOT in array sum to target

Find x not in array

In array, there are frequently asked questions “Two sum”, “three sum”, “find subarray with given sum”. Here is a target sum problem with a twist – find a number not in the array, but can make subarray sum equals to or close to the target. To solve “Find a number …

Continue reading

Combinations of adding parentheses – code

generate valid parentheses

To find number of possible combinations of valid parentheses, we have to know Catalan number. Catalan numbers are a sequence of natural numbers that follow the formula showing below. The first few Catalan numbers for n = 0, 1, 2, 3, 4, 5 … Cn = 1, 1, 2, 5, …

Continue reading

Combinations of adding operators and parentheses

possible expressions

Given a set of numbers, there are many different ways to add operators and parentheses to form an valid expression. The operations are +, -, *, /. The operator * and / have higher precedence than + and – . In infix expressions, we add parentheses to override the normal …

Continue reading

Permutation of multiple arrays and iterator – code


Permutation of multiple arrays and iterator has two tasks. First is the permutation of multiple arrays and output as an array of new combinations. The second is to implement the iterator of this new array. Permutation is an important topic in the fields of combinatorics. It is usually implemented using …

Continue reading

Royal succession order – code

royal succession order

Royal succession order is also known as the line of succession to the British throne. Under common law, the crown is inherited by a sovereign’s children or by a childless sovereign’s nearest collateral line. You can get the current British monarchy succession order here. As programmer, you might be asked …

Continue reading

Build nested object from hierarchical list- code

build nested object

Similar to convert JSON file to objects, we can covert hierarchical list to a nested object. The input is like a table of content with multiple levels, the output is a nested object obj.children[1].children[2]. “obj” is the top level object. Each nested object’s level maps with the level in the …

Continue reading

Find least common set – Outlier set

least common set

Given an array of sets, find the least common set. The least common set is also an outlier set, that has least in common with other sets. There are different ways to find the least common set. One ways is to use union-find to make all elements in a disjoint …

Continue reading

Sort with lambda comparator

Comparator is used to sort the objects by different attributes. You can simply put lambda in the place where requires Comparator. Lambda is preferred when the comparison is simple. Here are the syntax and examples in Java, JavaScript and Python. Sometime you may have complex comparisons involving multiple attributes. For example, …

Continue reading

Sort with Java Comparator

Java Comparator

Java Comparators are used to sort the user defined objects. It gives you flexibility to sort the objects in multiple ways. For example you can sort strings by alphabetical order, or by their lengths. If you have a People class with first name, last name and age, you can sort …

Continue reading

HashMap sort by key

hashmap object as key

In HashMap, the key can be any data type. The most common used data types are String and Integer. When you use custom defined object as key, you have to specify how to order them. In Java, you use TreeMap. In JavaScript and Python, you use lambda expressions.

Continue reading