{"id":25727,"date":"2021-12-04T09:19:40","date_gmt":"2021-12-04T03:49:40","guid":{"rendered":"https:\/\/python-programs.com\/?p=25727"},"modified":"2021-12-04T09:19:40","modified_gmt":"2021-12-04T03:49:40","slug":"python-program-for-friends-travel-problem","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-friends-travel-problem\/","title":{"rendered":"Python Program for Friends-Travel Problem"},"content":{"rendered":"

In this article, we’ll look at The Friends-Travel Problem, which is highly interesting.<\/p>\n

Friends-Travel Problem:<\/strong><\/p>\n

Assume n friends want to go to a party; they can travel alone or with another friend as a couple. We presume that n motorbikes are offered for n buddies.<\/p>\n

We need to figure out how many different ways the n friends can get to the party, either individually or in pairs of two as a couple.<\/p>\n

Answer:<\/strong><\/p>\n

One can either manually implement the naive technique using loops and if-else statements or use the quicker Recursion approach.<\/p>\n

Before going into the problem just have a glance over the recursion.<\/p>\n

To solve a larger problem, it is necessary to divide it into smaller difficulties.<\/p>\n

Let us consider the lower values of n like ( 1, 2, and 3).<\/p>\n

There are only one and two viable options for n = 1 and n = 2, respectively. And for n = 3, there are four alternative outcomes. How?<\/p>\n

For each value of n, a friend has two options: either travel alone or search for n-1 friends.<\/p>\n

Alternatively, the friend might choose a friend from the n-1 friends to travel with, and we will then look for n-2 friends.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given number of friends = 3<\/pre>\n

Output:<\/strong><\/p>\n

Different ways the 3 friends can get to the party =  4<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given number of friends = 5<\/pre>\n

Output:<\/strong><\/p>\n

Different ways the 5 friends can get to the party = 26<\/pre>\n

Program for Friends-Travel Problem in Python<\/h2>\n