## Add digits until number becomes a single digit

Write a program to add the digits of the given number until the number becomes a single digit number. Example: Input: 256, Output: 4 256 -> 13 -> 4 Input: 15, Output: 6 15 -> 6 Input: 82, Output: 1 82 -> 10 -> 1 Input: 242239, Output: 4 242239 -> 22 -> 4 Approach: … Read more

## Print all nested directories and files in a given directory – Recursion

Given a source directory, write a recursive program to print all the nested directories and files in the source directory. Example: [Hellojava] [target] original-Hellojava-1.0-SNAPSHOT.jar [classes] [com] [example] [lambda] [demo] Hello.class [maven-archiver] pom.properties Hellojava-1.0-SNAPSHOT.jar pom.xml Hellojava.iml [src] [test] [java] [main] [resources] [java] [com] Hello.java Approach: Check if given file is directory,  if yes then print its … Read more

## Print all subarrays using recursion

Given an array, write a recursive program to print all the subarrays. See the example below – Example:  Input [] = {1, 2, 3, 4} Output: [ 1 ] [ 1 2 ] [ 1 2 3 ] [ 1 2 3 4 ] [ 2 ] [ 2 3 ] [ 2 3 4 … Read more

## Given an array, find all unique subsets with a given sum with allowed repeated digits.

Objective: Given an array of integers and number N, Write an algorithm to find and print all the unique subsets of array for which sum is equal to N where array elements can be repeated any number of times.  Example: int [] arrA={2,4,3} sum =6 Output: [2, 2, 2] [2, 4] [3, 3] int [] … Read more

## Print all steps to convert one string to another string

Objective: Given two strings, source string and target string, which are permutation or anagram of each other. You are allowed two swap only consecutive characters. Write an algorithm to print all the steps ( all the swaps) which will lead the conversion of the source string to the target string. NOTE: There could be multiple … Read more

## Find all subsets of size K from a given number N (1 to N)

Objective: Given two integers N and K, Write an algorithm to find subsets of size K from the numbers 1 to N.  Example: N = 5 K = 3 Output: [1, 2, 3] [1, 2, 4] [1, 2, 5] [1, 3, 4] [1, 3, 5] [1, 4, 5] [2, 3, 4] [2, 3, 5] [2, … Read more

## Find all possible combinations with sum K from a given number N(1 to N) with the repetition of numbers is allowed

Objective: Given two integers N and K, Write an algorithm to find possible combinations that add to K, from the numbers 1 to N.  Condition: An integer from 1 to N can be repeated any number of times in combination. Example: N = 5 K = 3 Output: [1, 1, 1] [1, 2]  N … Read more

## Given an array, print all unique subsets with a given sum.

Objective: Given an array of integers and number N, Write an algorithm to find and print all the subsets of the array for which sum is equal to N. Example: input [] = {6,2,7,8,2,4,1,3,7,5} Sum = 8 Output: [1, 2, 2, 3] [1, 2, 5] [1, 3, 4] [1, 7] [2, 2, 4] [2, 6] … Read more

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

Objective: Given an integer N, Write an algorithm to select numbers from 1 to 9 and make combinations such that its sum to N.  Example: N= 5 Output:  [1, 4] [2, 3]  N =12 [1, 2, 3, 6] [1, 2, 4, 5] [1, 2, 9] [1, 3, 8] [1, 4, 7] [1, 5, 6] … Read more

## Find the number of distinct Islands OR connected components.

Objective: Given a 2d grid map of ‘1’s (land) and ‘0’s (water), count the number of distinct or unique islands. Island: 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 … Read more

## Depth-First Search (DFS) in 2D Matrix/2D-Array – Recursive Solution

Objective: Given a two-dimensional array or matrix, Do the depth-First Search (DFS) to print the elements of the given matrix. Implement the Depth-first traversal in a recursive manner. Example: int [][] grid = new int[][] { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}} Output: Depth-First Traversal: … Read more

## Number of Islands using BFS

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 grid, write an algorithm using Breadth-First Search(BFS) to … Read more

## Collatz Conjecture – Maximum Steps takes to transform (1, N) to 1.

Objective: Given a number N, write an algorithm to find the maximum number of steps it takes to transform (1, N) to 1 using Collatz Conjecture.  The Collatz conjecture is a conjecture in mathematics which states that no matter what value of Positive Number N, If the below sequence is followed then the sequence will … Read more

## Print Stack in reverse order.

Objective: Given a stack, write a program to print the stack elements in reverse order. Example: Approach: Use Temporary stack: Take temporary stack, and copy all the items from the given stack to a temporary stack. Elements will be stored in a temporary stack in reverse order. Now print elements from the temporary stack and … Read more

## Given an array, Print sum of all subsets

Objective: Given an array of numbers, write an algorithm to print all the subsets sum individually.   Example: Given Input: [1, 2] Output: 3 1 2 0 Explanation: subsets are , , , [1, 2] Given Input: [2, 4, 6] 12 6 8 2 10 4 6 0 Approach: Recursion Every element of the array has … Read more