Find all unique combinations of exact K numbers (from 1 to 9 ) with sum to N

Objective: Given two integers N and K. Write an algorithm to find all the combinations of k numbers which sum to N.  Conditions:  All K numbers must be between 1 and 9 and unique. All numbers in K are positive. Example: N= 5 K=2 Output: [1, 4] [2, 3] N =12 K=3 Output: [1, 2, … Read more

Print all subsets of an array with a sum equal to zero

Objective: Given an array of integers write an algorithm to find all the subsets for which sum is equal to zero. The array contains both positive and negative integers. Example: Given Array: [8, 3, 5, 1, -4, -8], required sum: 0 Output: [-8, -4, 1, 3, 8] [-8, 3, 5] [-8, 8] [-4, 1, 3] … Read more

Number of Islands

Objective: Given a 2d grid map of ‘1’s (land) and ‘0’s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. Assume all four edges of the grid are all surrounded by water. Given such a grid, write an algorithm to find the … Read more

Minimum No of operations required to convert a given number to 1 – Integer Replacement Problem.

Objective: Given a number N, write an algorithm to convert that number to 1. Below are the allowed operations. If N is even, do N = N/2. If N is odd, either do N =N – 1 or do N = N + 1 Example:  N = 16 Output: 4 16 → 8 → 4 … Read more

Round Price Problem

Problem Statement: When customers book an Airbnb the total price, which includes base price + service fee + cleaning fee. all these prices are in decimals. Write an algorithm to round each price such that the sum of the prices equals the round of the total sum of all decimal prices and minimize the rounding. … Read more

Reverse a Stack using recursion – In Place (Without using extra memory)

Objective: Given a Stack, write an algorithm to reverse the stack. Example: Original Stack: [14, 9, 67, 91, 101, 25] Reversed Stack: [25, 101, 91, 67, 9, 14] Approach: Use Recursion In this solution, we need two recursive functions. reverse() and insert_at_bottom(). reverse() – this function will be called by the driver. In this function, … Read more

Reverse a given number – Java Code

Objective: Given a number, write a java program to reverse a number Example: Given Number: 1234 Output: 4321 Given Number: 1020 Output: 201 Approach:  Using loop Keep divide the number by 10 and add the remainder to the reversed number (after multiple reversed number with 10) See the code. Run This Code Java Code: Output: … Read more

Generate all the strings of length n from 0 to k-1.

Objective: Given two numbers, n and k (k>=n), write an algorithm to generate all the strings of length n drawn from 0 – k-1. Example: k = 2, n = 2Output:  0 0      0 1         1 0         1 1 k = 3, n = 2 Output: 0 0         1 0        2 0        0 1         1 … Read more

Reverse a String using Recursion

Objective: Given a String, write a recursive program to reverse it. Example: Original String: tutorial horizon Reversed String: noziroh lairotut Approach: Take the first character out of the string, add it to the end of result of the remaining string. Make a recursive call to remaining string. Java Code: Run This Code public class ReverseString … Read more

Graph – Find Cycle in Undirected Graph using Disjoint Set (Union-Find)

Objective: Given a graph, check if the graph contains a cycle using disjoint set. Note: Disjoint-set data structure, also called a union–find data structure or merge–find set. Example: Earlier in Detect Cycle in Undirected Graph using DFS we discussed about how to find cycle in graph using DFS. In this article we will discuss how to find cycle using … Read more

Graph – Depth First Search using Recursion

Objective: Given a graph, do the depth first traversal using recursion. Earlier we have seen DFS using stack. In this article we will see how to do DFS using recursion. (Recursion also uses stack internally so more or less it’s same) What is depth-first traversal– Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One starts … Read more

Print all subarrays of a given array

Problem: Given an array write an algorithm to print all the possible sub-arrays. Example: int [] a = {1, 2, 3}; Output: Possible subarrays – {1}, {2}, {3}, {1, 2} , {2, 3}, {1, 2, 3} Approach: Click here to read about the recursive solution – Print all subarrays using recursion Use three nested loops. … Read more

Text Justification Problem (OR Word Wrap Problem)

Objec­tive:  Given a list of words and length L. Format the words so that each line will have only L characters and fully justified (left and right justified). Restrictions- You need to fit as many as words in every line. You are not allowed to split a word and put them in next line. Pad extra … Read more