# Tag Archives: Recursion

## Recursive Bubble SortFebruary 15, 2017

Background : Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.

## Generate all binary strings without consecutive 1’sJanuary 16, 2017

Given a integer K. Task is Print All binary string of size K (Given number). Examples: Input : K = 3 Output : 000 , 001 , 010 , 100 , 101 Input : K = 4 Output :0000 0001 0010 0100 0101 1000 1001 1010 Idea behind that is IF string ends with ‘1’… Read More »

## C++ program to implement Collatz ConjectureJanuary 15, 2017

Given a positive integer n, the task is to find whether this number reaches to 1 after performing following two operations:- If n is even, then n = n/2. If n is odd, then n = 3*n + 1. Repeat above steps, until it becomes 1. For example, for n = 12, we get the… Read More »

## RecursionJanuary 11, 2017

What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc. What is base… Read More »

## Program for Chocolate and Wrapper PuzzleJanuary 4, 2017

Given following three values, the task is to find the total number of maximum chocolates you can eat. money : Money you have to buy chocolates price : Price of a chocolate wrap : Number of wrappers to be returned for getting one extra chocolate. It may be assumed that all given values are positive… Read More »

## Print all longest common sub-sequences in lexicographical orderNovember 13, 2016

You are given two strings.Now you have to print all longest common sub-sequences in lexicographical order? Examples: Input : str1 = “abcabcaa”, str2 = “acbacba” Output: ababa abaca abcba acaba acaca acbaa acbca This problem is an extension of longest common subsequence. We first find length of LCS and store all LCS in 2D table… Read More »

## String with additive sequenceOctober 15, 2016

Given a string, the task is to find whether it contains an additive sequence or not. A string contains an additive sequence if its digits can make a sequence of numbers in which every number is addition of previous two numbers. A valid string should contain at least three digit to make one additive sequence.… Read More »

## Print sums of all subsets of a given setOctober 13, 2016

Given an array of integers, print sums of all subsets in it. Output sums can be printed in any order. Input : arr[] = {2, 3} Output: 0 2 3 5 Input : arr[] = {2, 4, 5} Output : 0 2 4 5 6 7 9 11 Method 1 (Recursive) We can recursively solve… Read More »

## Print all possible expressions that evaluate to a targetOctober 12, 2016

Given a string that contains only digits from 0 to 9, and an integer value, target. Find out how many expressions are possible which evaluate to target using binary operator +, – and * in given string of digits. Input : “123”, Target : 6 Output : {“1+2+3”, “1*2*3”} Input : “125”, Target : 7… Read More »

## Find all even length binary sequences with same sum of first and second half bitsAugust 2, 2016

Given a number n, find all binary sequences of length 2n such that sum of first n bits is same as sum of last n bits. Examples: Input: N = 2 Output: 0101 1111 1001 0110 0000 1010 Input: N = 3 Output: 011011 001001 011101 010001 101011 111111 110011 101101 100001 110101 001010 011110… Read More »

## Print all n-digit strictly increasing numbersJuly 29, 2016

Given number of digits n in a number, print all n-digit numbers whose digits are strictly increasing from left to right. Examples: Input: n = 2 Output: 01 02 03 04 05 06 07 08 09 12 13 14 15 16 17 18 19 23 24 25 26 27 28 29 34 35 36 37… Read More »