

Total number of non-decreasing numbers with n digits in JAVA
A non-decreasing number has every digit greater than or equal to the previous digit. Given the number of digits, find how many numbers...

Tiling Problem in JAVA
You can use a board that is 2 x n size. The tiles are of size 2 x 1. Count the number of ways to tile the board. Tiles can be placed...

Subset Sum Problem in JAVA
Given a set of non-negative integers, and a value sum, determine if there is a subset of the given set with the sum equal to the given...

Shortest Common Supersequence in JAVA
Given two strings as input, the task is to find the length of the shortest string that has both strings as subsequences. Solution: 1. ...

Palindrome Partitioning in JAVA
Starting from a string, we determine it’s a palindrome partitioning by diving it into substrings, each being a palindrome. In our problem...

Mobile Numeric Keypad Problem in JAVA
We use a mobile numeric keyboard. You can only press buttons that are up, left, right or down to the current button. You are not allowed...

Minimum number of squares whose sum equals to a given number in JAVA
Starting from a number, we must return the minimum number of square numbers whose sum equals to that number. Solution: 1. static int...

Minimum number of jumps to reach the end in JAVA
We start from an array of integers, where each number means how many steps can be made from that position. First, we are on index 0 of...

Minimum Initial Points to Reach Destination in JAVA
We have as an input a grid of cells with numbers. The numbers can be 0, positive or negative. To move across a cell, we must have...

Minimum Cost Polygon Triangulation in JAVA
You must find the minimum cost triangulation of a convex polygon. A triangulation is formed by drawing diagonals between nonadjacent...

Maximum sum rectangle in a 2D matrix in JAVA
Given a 2D array, find the maximum sum submatrix in it. Solution: 1. private static int maximumSumRectangle(int[][] matrix) { 2. ...

Maximum Sum Increasing Subsequence in JAVA
You are given an array of positive integers. You must write a program that will return the sum of the maximum sum subsequence of the...

Maximum profit by buying and selling a share at most twice in JAVA
A person buys shares in the morning and sells them in the same day. He can make maximum two transactions of this type in a day. The...

Maximum Length Chain of Pairs in JAVA
You receive as input n pairs of numbers. In every pair, the first number is smaller than the second one. A chain of pairs (a, b), (c, d)...

Longest Palindromic Substring in JAVA
Starting from a string, we have to find the longest substring which is a palindrome. Solution: 1. static String...

Longest Palindromic Subsequence in JAVA
Given a sequence of letters, find the length of the longest palindromic subsequence in it. Solution: 1. public static void main(String[]...

Longest Even Length Substring such that the Sum of the First and Second Halves is the same in JAVA
The input is a string of digits. You must find the longest substring which first half digits sum is equal to the right half digits sum....

Longest Bitonic Subsequence in JAVA
A Bitonic Subsequence is a part of a sequence that is formed first by increasing numbers, and after that by decreasing numbers. Write a...

Largest Sum Contiguous Subarray (Kadane's Algorithm) in JAVA
Given an integer array, you have to find a contiguous subarray with the largest sum, and return that sum. Solution: 1. public static...