Monday, August 28, 2023

Recursion in DS | Data Structure

In this article we will solve different recursions related problems



Indirect Recursion

In Indirect function A call function B and Function B call function till a specific condition

Here we are solving and issue using Indirect recursion where a person buying apples from shop.


const Person=(m,packet)=>
{
console.log(packet,"apples, remaining money :",m)
if(m===0)
{
return 1
}
apple(m)
}


let packet=0;
const apple=m=>Person(m-10,packet+10)
Person(100,0)



Problem

Print all Possible combination of elements that is stored in array



const data=[1,2,3,4]
// for(let i=0;i<data.length;i++)
// {
// for(let j=i+1;j<data.length;j++)
// {
// console.log([data[i],data[j]])
// }
// }


function OuterCombination(data,i)
{
if(i===data.length) return
Inner(data,i,i+1)
OuterCombination(data,i+1)

}
function Inner(data,i,j)
{
if(j===data.length) return
console.log([data[i],data[j]])
Inner(data,i,j+1)
}

OuterCombination(data,0)

//outPut
// [ 1, 2 ]
// [ 1, 3 ]
// [ 1, 4 ]
// [ 2, 3 ]
// [ 2, 4 ]
// [ 3, 4 ]



Previous Post
Next Post

post written by:

0 comments: