You are on page 1of 24

Recursion

Codes
Fibonacci Recursive Run on IDE
#include <iostream>

using namespace std;

int fib(int n){

if(n==0||n==1){

return n;

int f1 = fib(n-1);

int f2 = fib(n-2);

return f1 + f2;

int main() {

int n;
cin>>n;

cout<<fib(4)<<endl;

return 0;

Power/FastPower Run on IDE


#include <iostream>

using namespace std;

int power(int a,int b){

if(b==0){

return 1;

else{

return a*power(a,b-1);

int fastPower(int a,int b){

if(b==0){

return 1;

int smallAns = fastPower(a,b/2);

smallAns *= smallAns;

if(b&1){

return a*smallAns;

return smallAns;

int main() {

int a,b;

cin>>a>>b;

cout<<power(a,b)<<endl;

cout<<fastPower(5,5)<<endl;

return 0;

Array is Sorted or Not Run on IDE


#include <iostream>

using namespace std;

bool isSorted(int *a,int n){

if(n==1){

return true;

}
if(a[0]<a[1] && isSorted(a+1,n-1)){

return true;

return false;

int main() {

int a[] = {1,2,13,5,7};

int n = 5;

if(isSorted(a,n)){

cout<<"Yes Sorted!";

else{

cout<<"Not Sorted!";

return 0;

Recursive Bubble Sort Run on IDE


#include <iostream>

using namespace std;


void bubbleSort(int a[],int n){

//Base Case

if(n==1){

return;

//Sort 1 element, and call for remaining n-1 elements

for(int j=0;j<=n-2;j++){

if(a[j]>a[j+1]){

swap(a[j],a[j+1]);

bubbleSort(a,n-1);

void bubbleSort2(int a[],int j,int n){

//Base Case

if(n==1){

return;

if(j==n-1){

return bubbleSort2(a,0,n-1);

//Rec Case

if(a[j]>a[j+1]){
swap(a[j],a[j+1]);

bubbleSort2(a,j+1,n);

return;

int main() {

int a[] = {5,4,3,2,1};

int n = 5;

bubbleSort(a,n);

for(int i=0;i<n;i++){

cout<<a[i]<<" ";

return 0;

Increasing Decreasing Number Run on


IDE
#include <iostream>

using namespace std;

void dec(int n){

if(n==0){
return;

cout<<n<<" ";

dec(n-1);

void inc(int n){

if(n==0){

return;

inc(n-1);

cout<<n<<" ";

int main() {

int n=5;

dec(n);

cout<<endl;

inc(n);

Linear Search Run on IDE


#include <iostream>

using namespace std;


int linearSearch(int *a,int i,int n,int key){

if(i==n){

return -1;

if(a[i]==key){

return i;

return linearSearch(a,i+1,n,key);

int main() {

int a[] = {1,3,2,5,6,9};

int n = sizeof(a)/sizeof(int);

int key = 15;

cout<<linearSearch(a,0,n,key);

return 0;

2048 Print Problem Run on IDE


#include <iostream>

using namespace std;

char spellings[][10] = {"zero","one","two","three","four","fi


ve","six","seven","eight","nine"};

void print(int n){

if(n==0){

return;

print(n/10);

cout<<spellings[n%10]<<" ";

int main() {

int n;

cin>>n;

print(n);

Merge Sort Tutorial Run on IDE


Watch the Video Tutorial
#include<iostream>

using namespace std;

void merge(int *a,int s,int e){

int mid = (s+e)/2;

int i = s;

int j = mid+1;

int k = s;

int temp[100];

while(i<=mid && j<=e){

if(a[i] < a[j]){

temp[k++] = a[i++];

else{

temp[k++] = a[j++];

while(i<=mid){

temp[k++] = a[i++];

while(j<=e){

temp[k++] = a[j++];


//We need to copy all element to original arrays

for(int i=s;i<=e;i++){

a[i] = temp[i];

void mergeSort(int a[],int s,int e){

//Base case - 1 or 0 elements

if(s>=e){

return;

//Follow 3 steps

//1. Divide

int mid = (s+e)/2;

//Recursively the arrays - s,mid and mid+1,e

mergeSort(a,s,mid);

mergeSort(a,mid+1,e);

//Merge the two parts

merge(a,s,e);


int main(){

int a[100];

int n;

cin>>n;

for(int i=0;i<n;i++){

cin>>a[i];

mergeSort(a,0,n-1);

for(int i=0;i<n;i++){

cout<<a[i]<<" , ";

Permutations of a String Run on IDE


#include <iostream>

using namespace std;

void permute(char*in,int i){

//Base case
if(in[i]=='\0'){

cout<<in<<" ,";

return;

//Recursive Case

for(int j=i;in[j]!='\0';j++){

swap(in[i],in[j]);

permute(in,i+1);

//Backtracking - To restore the original array

swap(in[i],in[j]);

int main() {

char in[100];

cin>>in;

permute(in,0);

return 0;

Phone Keypad Run on IDE


#include <iostream>

#include <cstring>

using namespace std;

char keypad[][10] = {"","","ABC","DEF","GHI","JKL","MNO","PQR

S","TUV","WXYZ"};

void printKeypadString(char *in,char *out,int i,int j){

//Base Case

if(in[i]=='\0'){

out[j] = '\0';

cout<<out<<",";

return;

//Rec Case

int digit = in[i]-'0';

if(digit==1||digit==0){

printKeypadString(in,out,i+1,j);

for(int k=0;keypad[digit][k]!='\0';k++){

out[j] = keypad[digit][k];

printKeypadString(in,out,i+1,j+1);

int main() {

char in[100];

char out[100];

cin>>in;

printKeypadString(in,out,0,0);

Rat in a Maze Run on IDE


#include<iostream>

using namespace std;

bool ratInMaze(char maze[10][10],int sol[][10],int i,int j,in

t m,int n){

if(i==m && j==n){

sol[i][j] = 1;

//Print the Path

for(int x=0;x<=m;x++){

for(int y=0;y<=n;y++){

cout<<sol[x][y]<<" ";
}

cout<<endl;

cout<<endl;

return true;

// Rec Case

if(i>m || j>n){

return false;

if(maze[i][j]=='X'){

return false;

//Assumption

sol[i][j] = 1;

bool rightSuccess = ratInMaze(maze,sol,i+1,j,m,n);

bool downSuccess = ratInMaze(maze,sol,i,j+1,m,n);

sol[i][j] = 0;

if(rightSuccess||downSuccess){

return true;

}
return false;

int main(){

char maze[10][10] = {

"0000",

"00X0",

"000X",

"0X00",

};

int soln[10][10] = {0};

bool ans = ratInMaze(maze,soln,0,0,3,3);

if(ans==false){

cout<<"NO path exists !";

return 0;

Sudoku Solver Run on IDE


#include <iostream>

#include <cmath>

using namespace std;


bool canPlace(int mat[][9],int i,int j,int n,int number){

for(int x=0;x<n;x++){

//Row and Colm Check

if(mat[x][j]==number||mat[i][x]==number){

return false;

int rn = sqrt(n);

int sx = (i/rn)*rn;

int sy = (j/rn)*rn;

for(int x=sx;x<sx+rn;x++){

for(int y=sy;y<sy+rn;y++){

if(mat[x][y]==number){

return false;

return true;

bool solveSudoku(int mat[][9],int i,int j,int n){

//Base Case

if(i==n){

//Print the Matrix

for(int i=0;i<n;i++){
for(int j=0;j<n;j++){

cout<<mat[i][j]<<" ";

cout<<endl;

return true;

// Case Row End

if(j==n){

return solveSudoku(mat,i+1,0,n);

//Skip the Pre-filled Cells

if(mat[i][j]!=0){

return solveSudoku(mat,i,j+1,n);

//Rec Case

//Fill the current cell with possible options

for(int number=1;number<=n;number++){

if(canPlace(mat,i,j,n,number)){

//Assume

mat[i][j] = number;

bool couldWeSolve = solveSudoku(mat,i,j+1,n);

if(couldWeSolve==true){

return true;

}
}

//Backtrack here

mat[i][j] = 0;

return false;

int main() {

int mat[9][9] =

{5,3,0,0,7,0,0,0,0},

{6,0,0,1,9,5,0,0,0},

{0,9,8,0,0,0,0,6,0},

{8,0,0,0,6,0,0,0,3},

{4,0,0,8,0,3,0,0,1},

{7,0,0,0,2,0,0,0,6},

{0,6,0,0,0,0,2,8,0},

{0,0,0,4,1,9,0,0,5},

{0,0,0,0,8,0,0,7,9}

};

solveSudoku(mat,0,0,9);

return 0;

}
Subsequences Run on IDE
#include <iostream>

using namespace std;

void subsequences(char *in,char *out,int i,int j){

//Base case

if(in[i]=='\0'){

out[j] = '\0';

cout<<out<<",";

return;

//Rec Case

// 1.Include the Current Char

out[j] = in[i];

subsequences(in,out,i+1,j+1);

// 2. Exclude the Current Char

subsequences(in,out,i+1,j);

int main() {

char in[100];

cin>>in;

char out[100];
subsequences(in,out,0,0);

return 0;

Multiply Without * Run on IDE


#include<iostream>

using namespace std;

// function to multiply two numbers x and y

int multiply(int x, int y)

// 0 multiplied with anything gives 0

if(y == 0)

return 0;

// Add x one by one

if(y > 0 )

return (x + multiply(x, y-1));

// the case where y is negative

if(y < 0 )

return -multiply(x, -y);


int main()

int x=5,y=-11;

cout<<multiply(x,y);

return 0;

String To Int Run on IDE


#include<iostream>

#include<cstring>

using namespace std;

int stringToInt(char*a,int n){

if(n==0){

return 0;

int last_digit = a[n-1] - '0';

return stringToInt(a,n-1)*10 + last_digit;

int main(){

char a[] = "1245";

int n = strlen(a);
cout<<stringToInt(a,n)<<endl;

Tiling Problem Run on IDE


#include <bits/stdc++.h>

using namespace std;

long long int noOfWays(long long int n){

if(n==1 || n==2){

return n;

return noOfWays(n-1) + noOfWays(n-2);

int main() {

//code

long long int n;

cin>>n;

cout<<noOfWays(n)<<endl;

return 0;

You might also like