# All posts tagged algorithms

Problem statement Pots of gold game: Two players and . There are pots of gold arranged in a line, each containing some gold coins (the players can see how many coins are there in each gold pot – perfect information). They get alternating turns in which the player can pick . . . Read more

You have types of lego blocks, of sizes given as , and . Assume that you have an infinite number of blocks of each type. Using these blocks, you want to make a wall of height and width . The wall should not have any holes in it. The wall . . . Read more

Problem statement Given an array of numbers, find the number of pairs in an array whose difference is Input Format The first line of the input will contain and . The next line will contain space separated integers. Output Format The number of pairs in array whose difference is . . . . Read more

Problem statement Given an array of numbers, find the longest increasing continuous subsequence. Sample Input 3 4 1 2 3 3 4 5 10 99 100 80 Sample Output 3 4 5 10 99 100 Solution public static int[] longestIncreasingContSubSq(int[] arr) { int subSeqLength = 1; int longest = 1; . . . Read more

In a previous post I illustrated how we can enumerate all the prime numbers up to a given range using a well-known approach called Sieve of Eratosthenes. But what if we want to enumerate all the prime numbers within a given interval ? Instead of just crossing off multiples of . . . Read more

In this post I will explain a less intuitive approach of implementing a ceil() function in Java, that is faster than known comparison approaches. A few known facts Fact 1 Dividing two integers and in Java will always floor the result: int a, b; int ans = a/b; // is . . . Read more

Mr. Twotwo as the name suggests is very fond powers of . Moreover he also has special affinity to number . He is known for carrying quirky experiments on powers of . One day he played a game in his class. He brought some number plates on each of which . . . Read more

Problem statement You are given sticks, where the length of each stick is a positive integer. A cut operation is performed on the sticks such that all of them are reduced by the length of the smallest stick. Suppose we have six sticks of the following lengths: 5 4 4 . . . Read more

Problem statement A peak element is an element that is greater than its neighbors. Given an input array where , find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. You may . . . Read more

Problem statement Swap two numbers and without using a third variable. Solution public class SwapNumbers { public static void main(String[] args) { int a = 6; int b = 2; a = a + b; b = a – b; a = a – b; } }