Given an array containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.

# Algorithms

## LeetCode – Intersection of Two Arrays (Java)

Given two arrays, write a function to compute their intersection.

## LeetCode – Power of Four (Java)

Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Java Solution 1 – Naive Iteration public boolean isPowerOfFour(int num) { while(num>0){ if(num==1){ return true; } if(num%4!=0){ return false; }else{ num=num/4; } } return false; }public boolean isPowerOfFour(int num) { while(num>0){ if(num==1){ return true; } … Read more

## LeetCode – Integer Break (Java)

Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + … Read more

## LeetCode – Coin Change (Java)

Given a set of coins and a total money amount. Write a method to compute the smallest number of coins to make up the given amount. If the amount cannot be made up by any combination of the given coins, return -1.

For example:

Given [2, 5, 10] and amount=6, the method should return -1.

Given [1, 2, 5] and amount=7, the method should return 2.

## Longest Common Substring (Java)

In computer science, the longest common substring problem is to find the longest string that is a substring of two or more strings.

## LeetCode – Reconstruct Itinerary (Java)

Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK.

## LeetCode – Self Crossing (Java)

Analysis This problem can be easily solved if the three self crossing cases are summarized well. Here are the three self crossing cases. There are no other self crossing situations based on the restrictions of counter-clockwise. Java Solution Writing the solution is straightforward once the 3 self crossing cases are identified. public boolean isSelfCrossing(int[] x) … Read more

## LeetCode – House Robber III (Java)

The houses form a binary tree. If the root is robbed, its left and right can not be robbed. Analysis Traverse down the tree recursively. We can use an array to keep 2 values: the maximum money when a root is selected and the maximum value when a root if NOT selected. Java Solution public … Read more

## LeetCode – Counting Bits (Java)

Given a non negative integer number num. For every numbers i in the range 0 â‰¤ i â‰¤ num calculate the number of 1’s in their binary representation and return them as an array. Example: For num = 5 you should return [0,1,1,2,1,2]. 1. Naive Solution We can simply count bits for each number like … Read more

## LeetCode – Odd Even Linked List (Java)

**Problem **

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.

## Implement a Stack Using an Array in Java

This post shows how to implement a stack by using an array.

The requirements of the stack are: 1) the stack has a constructor which accepts a number to initialize its size, 2) the stack can hold any type of elements, 3) the stack has a push() and a pop() method.

## LeetCode – Queue Reconstruction by Height (Java)

Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h. Write an … Read more

## Rotate Array in Java

Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. How many different ways do you know to solve this problem? Solution 1 – Intermediate Array In a straightforward way, we can create a new array … Read more

## LeetCode – Increasing Triplet Subsequence (Java)

Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Examples: Given [1, 2, 3, 4, 5], return true. Given [5, 4, 3, 2, 1], return false. Analysis This problem can be formalized as finding a sequence x, y and z, such that x < y < ... Read more