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 = 4
Output : 5
Explanation : 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 = 5
Output : 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;
int ways( int n)
{
if (n == 1) {
return 1;
}
if (n == 2) {
return 2;
}
else {
return ways(n - 1) + ways(n - 2);
}
}
int main()
{
int n = 5;
cout << "Number of ways = " << ways(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int ways( int n)
{
if (n == 1 )
{
return 1 ;
}
if (n == 2 )
{
return 2 ;
}
else
{
return ways(n - 1 ) + ways(n - 2 );
}
}
public static void main (String[] args)
{
int n = 5 ;
System.out.println( "Number of ways = " + ways(n));
}
}
|
Python3
def ways(n) :
if (n = = 1 ) :
return 1 ;
if (n = = 2 ) :
return 2 ;
else :
return ways(n - 1 ) + ways(n - 2 );
if __name__ = = "__main__" :
n = 5 ;
print ( "Number of ways = " , ways(n));
|
C#
using System;
class GFG
{
static int ways( int n)
{
if (n == 1)
{
return 1;
}
if (n == 2)
{
return 2;
}
else
{
return ways(n - 1) + ways(n - 2);
}
}
public static void Main()
{
int n = 5;
Console.WriteLine( "Number of ways = " + ways(n));
}
}
|
Javascript
<script>
function ways(n)
{
if (n == 1)
{
return 1;
}
if (n == 2)
{
return 2;
}
else
{
return ways(n - 1) + ways(n - 2);
}
}
let n = 5;
document.write( "Number of ways = " + ways(n));
</script>
|
Output:
Number of ways = 8
Time Complexity: O(2^N)
Auxiliary Space: O(N)
Another Approach:
We can store the computed values in an array and reuse the computed value afterwords which would make the program efficient.
Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > dp;
int ways( int n)
{
int & res = dp[n];
if (res!=-1)
return res;
if (n == 1) {
return res=1;
}
if (n == 2) {
return res=2;
}
else {
return res = ways(n - 1) + ways(n - 2);
}
}
int main()
{
int n = 5;
dp = vector< int >(n + 1,-1);
cout << "Number of ways = " << ways(n);
return 0;
}
|
Java
class GFG {
static int [] dp;
static int ways( int n)
{
int res = dp[n];
if (res != - 1 )
return res;
if (n == 1 ) {
return res = 1 ;
}
if (n == 2 ) {
return res = 2 ;
}
else {
return res = ways(n - 1 ) + ways(n - 2 );
}
}
public static void main(String[] args)
{
int n = 10 ;
dp = new int [n + 1 ];
Arrays.fill(dp, - 1 );
System.out.println( "Number of ways = " + ways(n));
}
}
|
Python3
dp = []
def ways(n):
res = dp[n]
if (res! = - 1 ):
return res
if (n = = 1 ) :
res = 1
return res
if (n = = 2 ) :
res = 2
return res
else :
res = ways(n - 1 ) + ways(n - 2 )
return res
if __name__ = = '__main__' :
n = 5
dp = [ - 1 ] * (n + 1 )
print ( "Number of ways =" ,ways(n))
|
C#
using System;
class Program {
static int [] dp;
static int Ways( int n) {
int res = dp[n];
if (res!=-1) {
return res;
}
if (n == 1) {
res=1;
return res;
}
if (n == 2) {
res=2;
return res;
} else {
res = Ways(n - 1) + Ways(n - 2);
return res;
}
}
static void Main( string [] args) {
int n = 5;
dp = new int [n + 1];
for ( int i = 0; i <= n; i++) {
dp[i] = -1;
}
Console.WriteLine( "Number of ways = " + Ways(n));
}
}
|
Javascript
<script>
let dp = [];
function ways(n)
{
let res = dp[n];
if (res!=-1)
return res;
if (n == 1) {
res = 1;
return res;
}
if (n == 2) {
res = 2;
}
else {
res = ways(n - 1) + ways(n - 2);
}
dp[n] = res;
return res;
}
let n = 5;
dp = new Array(n+1).fill(-1);
document.write(dp);
document.write( "Number of ways = " + ways(n));
</script>
|
Output
Number of ways = 8
Time Complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
14 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...