# Category: Recursion

## 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...

## 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...

## 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...

## 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,...

## Collatz Conjecture – Steps to transform Number to 1

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 always reach 1. If N...

## 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...

## 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...

## Print Numbers from 1 to N without using loop

Objective: Given a number N, write an program to print from number 1 to N without using loop. Example: N = 20 Output: 1 2 3 4 5 6 7 8 9 10 11 12...

## 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...

## 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...

## 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...

## Print all subarrays of a given array

Objec­tive:  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,...

## 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...

## Divide and Conquer – Rearrange array elements in special order

Objec­tive:  Given an array of integers of size 2n, write an algorithm to arrange them such that first n elements and last n elements are set up in alternative manner. Say n = 3 and...

## Find the local minima in a given array

Objec­tive:  Given an array of integer write an algorithm to find the local minima. Local Minima: An element is considered as local minima if it is less than both of its neighbors (if neighbors exist)....