# Count possible combinations of pairs with adjacent elements from first N numbers

Given a number N, the task is to count all possible combinations of pairs formed using adjacent elements.

**Note**: If an element exists already in a pair, it cannot be picked in the next pair. For example: for {1,2,3}: {1,2} and {2,3} will not be considered as a correct combination.

**Examples:**

Input :N = 4Output :5Explanation :If N = 4, the possible combinations are: {1}, {2}, {3}, {4} {1, 2}, {3, 4} {1}, {2, 3}, {4} {1}, {2}, {3, 4} {1, 2}, {3}, {4}Input :N = 5Output :8

**Approach : ** Break the problem into smaller subproblems. If there are N numbers, and there are two cases either a number is alone, or it is in a pair, if a number is alone, find the ways of pairing (n-1) numbers left, or if it is in a pair, find the ways of pairing (n-2) numbers left. If there are just 2 numbers left they can generate 2 combinations either alone or in a pair, and if a single number is left it will be in singleton, so there is just 1 combination.

Below is the implementation of the above approach:

## C++

`#include <bits/stdc++.h> ` `using` `namespace` `std; ` `// Function to count the number of ways ` `int` `ways(` `int` `n) ` `{ ` ` ` `// If there is a single number left ` ` ` `// it will form singleton ` ` ` `if` `(n == 1) { ` ` ` `return` `1; ` ` ` `} ` ` ` `// if there are just 2 numbers left, ` ` ` `// they will form a pair ` ` ` `if` `(n == 2) { ` ` ` `return` `2; ` ` ` `} ` ` ` `else` `{ ` ` ` `return` `ways(n - 1) + ways(n - 2); ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` ` ` `cout << ` `"Number of ways = "` `<< ways(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`/*package whatever //do not write package name here */` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to count the number of ways ` `static` `int` `ways(` `int` `n) ` `{ ` ` ` `// If there is a single number left ` ` ` `// it will form singleton ` ` ` `if` `(n == ` `1` `) ` ` ` `{ ` ` ` `return` `1` `; ` ` ` `} ` ` ` ` ` `// if there are just 2 numbers left, ` ` ` `// they will form a pair ` ` ` `if` `(n == ` `2` `) ` ` ` `{ ` ` ` `return` `2` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `return` `ways(n - ` `1` `) + ways(n - ` `2` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` ` ` `System.out.println(` `"Number of ways = "` `+ ways(n)); ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code implementation of the above program ` ` ` `# Function to count the number of ways ` `def` `ways(n) : ` ` ` ` ` `# If there is a single number left ` ` ` `# it will form singleton ` ` ` `if` `(n ` `=` `=` `1` `) : ` ` ` `return` `1` `; ` ` ` ` ` `# if there are just 2 numbers left, ` ` ` `# they will form a pair ` ` ` `if` `(n ` `=` `=` `2` `) : ` ` ` `return` `2` `; ` ` ` ` ` `else` `: ` ` ` `return` `ways(n ` `-` `1` `) ` `+` `ways(n ` `-` `2` `); ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `5` `; ` ` ` ` ` `print` `(` `"Number of ways = "` `, ways(n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above code ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to count the number of ways ` `static` `int` `ways(` `int` `n) ` `{ ` ` ` `// If there is a single number left ` ` ` `// it will form singleton ` ` ` `if` `(n == 1) ` ` ` `{ ` ` ` `return` `1; ` ` ` `} ` ` ` ` ` `// if there are just 2 numbers left, ` ` ` `// they will form a pair ` ` ` `if` `(n == 2) ` ` ` `{ ` ` ` `return` `2; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `return` `ways(n - 1) + ways(n - 2); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 5; ` ` ` ` ` `Console.WriteLine(` `"Number of ways = "` `+ ways(n)); ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

Number of ways = 8

## Recommended Posts:

- Minimize the count of adjacent pairs with different parity
- Count of array elements which is smaller than both its adjacent elements
- Count of subsets of integers from 1 to N having no adjacent elements
- Count of all subsequences having adjacent elements with different parity
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K
- Count of decreasing pairs formed from numbers 1 to N
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count numbers in a range that are divisible by all array elements
- Print all possible combinations of r elements in a given array of size n
- Smallest number possible by swapping adjacent even odd pairs
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Rearrange numbers in an array such that no two adjacent numbers are same
- Count of 0s to be flipped to make any two adjacent 1s at least K 0s apart
- Ways to fill N positions using M colors such that there are exactly K pairs of adjacent different colors
- Convert one array to another using adjacent swaps of elements
- Maximum product of 4 adjacent elements in matrix
- Maximum subsequence sum of at most K-distant adjacent elements
- Distinct adjacent elements in a binary array
- Maximum sum such that exactly half of the elements are selected and no two adjacent

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.