# Category Archives: Algorithms

We have discussed Dynamic Programming solution for Longest Increasing Subsequence problem in this post and a O(nLogn) solution in this post. Following are commonly asked… Read More
Given a number, find the next smallest palindrome larger than this number. For example, if the input number is “2 3 5 4 5”, the… Read More
Given an array of N elements, where each element is at most K away from its target position, devise an algorithm that sorts in O(N… Read More
Given an array of integers, find anyone combination of four elements in the array whose sum is equal to a given value X. For example, … Read More
Given an array of integers, find all combination of four elements in the array whose sum is equal to a given value X. For example, if… Read More
Given two signed integers, write a function that returns true if the signs of given integers are different, otherwise false. For example, the function should… Read More
You are given n pairs of numbers. In every pair, the first number is always smaller than the second number. A pair (c, d) can… Read More
Given a sequence of words, and a limit on the number of characters that can be put in one line (line width). Put line breaks… Read More
Given a function foo() that returns integers from 1 to 5 with equal probability, write a function that returns integers from 1 to 7 with… Read More
Deterministic Finite Automaton (DFA) can be used to check whether a number “num” is divisible by “k” or not. If the number is not divisible,… Read More
Write a function that calculates the day of the week for any particular date in the past or future. A typical application is to calculate… Read More
Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.  Example: … Read More
Given an unsorted array and a number n, find if there exists a pair of elements in the array whose difference is n. Examples:  Input: arr[]… Read More
A magic square of order n is an arrangement of n2 numbers, usually distinct integers, in a square, such that the n numbers in all… Read More
Given a number n, write a function that returns count of numbers from 1 to n that don’t contain digit 3 in their decimal representation. Examples:  … Read More