Open In App

Count of possible permutations of a number represented as a sum of 2’s, 4’s and 6’s only

Given an integer N, the task is to find out the number of permutations in which N can be represented as a sum of 2s, 4s, and 6s only.
Note: The different permutations of the same combination will also be counted.
Examples: 
 

Input: N = 8 
Output:
Explanation: The possible combinations are: 
2 2 2 2 
4 4 
4 2 2 
2 2 4 
2 4 2 
2 6 
6 2 
Input: N = 6 
Output:
 

 

Approach: In order to solve this problem, we are using a Dynamic Programming approach: 
 

dp[2] = 1 
dp[4] = 2 { 4 can be represented as 2+2, 4} 
dp[6] = 4 { 6 can be represented as 2+2+2, 2+4, 4+2, 6} 
dp[i] = dp[i-2]+dp[i-4] + dp[i – 6] for all even values of i in the range [8, N] 
 
 

Below is the implementation of the above approach:
 




// C++ code for above implementation
 
#include <bits/stdc++.h>
using namespace std;
 
// Returns number of ways
// to reach score n
int count(int n)
{
    // table[i] will store count
    // of solutions for value i.
    if (n == 2)
        return 1;
    else if (n == 4)
        return 2;
    else if (n == 6)
        return 4;
 
    int table[n + 1], i;
 
    // Initialize all table
    // values as 0
    for (i = 0; i < n + 1; i++)
        table[i] = 0;
 
    // Base case (If given value
    // is 0, 1, 2, or 4)
    table[0] = 0;
    table[2] = 1;
    table[4] = 2;
    table[6] = 4;
 
    for (i = 8; i <= n; i = i + 2) {
 
        table[i] = table[i - 2]
                   + table[i - 4]
                   + table[i - 6];
    }
 
    return table[n];
}
 
// Driver Code
int main(void)
{
    int n = 8;
    cout << count(n);
 
    return 0;
}




// Java code for above implementation
import java.util.*;
class GFG{
 
// Returns number of ways
// to reach score n
static int count(int n)
{
    // table[i] will store count
    // of solutions for value i.
    if (n == 2)
        return 1;
    else if (n == 4)
        return 2;
    else if (n == 6)
        return 4;
 
    int table[] = new int[n + 1];
    int i;
 
    // Initialize all table
    // values as 0
    for (i = 0; i < n + 1; i++)
        table[i] = 0;
 
    // Base case (If given value
    // is 0, 1, 2, or 4)
    table[0] = 0;
    table[2] = 1;
    table[4] = 2;
    table[6] = 4;
 
    for (i = 8; i <= n; i = i + 2)
    {
        table[i] = table[i - 2] +
                   table[i - 4] +
                   table[i - 6];
    }
 
    return table[n];
}
 
// Driver Code
public static void main(String args[])
{
    int n = 8;
    System.out.print(count(n));
}
}
 
// This code is contributed by Akanksha_Rai




# Python3 code for above implementation
 
# Returns number of ways
# to reach score n
def count(n) :
 
    # table[i] will store count
    # of solutions for value i.
    if (n == 2) :
        return 1;
    elif (n == 4) :
        return 2;
    elif (n == 6) :
        return 4;
 
    table = [0] * (n + 1);
 
    # Initialize all table
    # values as 0
    for i in range(n + 1) :
        table[i] = 0;
 
    # Base case (If given value
    # is 0, 1, 2, or 4)
    table[0] = 0;
    table[2] = 1;
    table[4] = 2;
    table[6] = 4;
 
    for i in range(8 , n + 1, 2) :
 
        table[i] = table[i - 2] + \
                   table[i - 4] + \
                   table[i - 6];
 
    return table[n];
 
# Driver Code
if __name__ == "__main__" :
 
    n = 8;
    print(count(n));
 
# This code is contributed by AnkitRai01




// C# code for above implementation
using System;
class GFG{
 
// Returns number of ways
// to reach score n
static int count(int n)
{
    // table[i] will store count
    // of solutions for value i.
    if (n == 2)
        return 1;
    else if (n == 4)
        return 2;
    else if (n == 6)
        return 4;
 
    int []table = new int[n + 1];
    int i;
 
    // Initialize all table
    // values as 0
    for (i = 0; i < n + 1; i++)
        table[i] = 0;
 
    // Base case (If given value
    // is 0, 1, 2, or 4)
    table[0] = 0;
    table[2] = 1;
    table[4] = 2;
    table[6] = 4;
 
    for (i = 8; i <= n; i = i + 2)
    {
        table[i] = table[i - 2] +
                   table[i - 4] +
                   table[i - 6];
    }
 
    return table[n];
}
 
// Driver Code
public static void Main()
{
    int n = 8;
    Console.Write(count(n));
}
}
 
// This code is contributed by Code_Mech




<script>
// Javascript program to implement
// the above approach
 
// Returns number of ways
// to reach score n
function count(n)
{
    // table[i] will store count
    // of solutions for value i.
    if (n == 2)
        return 1;
    else if (n == 4)
        return 2;
    else if (n == 6)
        return 4;
   
    let table = Array.from({length: n+1}, (_, i) => 0);
    let i;
   
    // Initialize all table
    // values as 0
    for (i = 0; i < n + 1; i++)
        table[i] = 0;
   
    // Base case (If given value
    // is 0, 1, 2, or 4)
    table[0] = 0;
    table[2] = 1;
    table[4] = 2;
    table[6] = 4;
   
    for (i = 8; i <= n; i = i + 2)
    {
        table[i] = table[i - 2] +
                   table[i - 4] +
                   table[i - 6];
    }
   
    return table[n];
}
  
  // Driver Code
     
    let n = 8;
    document.write(count(n));
  
 // This code is contributed by susmitakundugoaldanga.
</script>

Output
7



Time Complexity: O(N) 
Auxiliary Space Complexity: O(N)
 

Efficient approach : Space optimization O(1)

In previous approach we the current value dp[i] is only depend upon the previous 3 values i.e. dp[i-2], dp[i-4] and dp[i-6]. So to optimize the space we can keep track of previous and current values by the help of variables prev2, prev4, prev6 and curr which will reduce the space complexity from O(N) to O(1).  

Implementation Steps:

Implementation:




// C++ program for above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Returns number of ways
// to reach score n
int count(int n)
{
    // base cases
    if (n == 2) return 1;
    if (n == 4) return 2;
    if (n == 6) return 4;
 
    // initialize variables
    int sum = 0, i, prev2 = 1, prev4 = 2, prev6 = 4;
 
    // iterate for all possible
    // values of i
    for (i = 8; i <= n; i += 2) {
        sum = prev2 + prev4 + prev6;
        prev2 = prev4;
        prev4 = prev6;
        prev6 = sum;
    }
 
    // return the number of ways to reach n
    return sum;
}
 
int main(void)
{
    int n = 8;
    cout << count(n);
    return 0;
}
// --- by bhardwajji




// Java program for above approach
 
import java.io.*;
 
class Main {
  // Returns number of ways
// to reach score n
static int count(int n)
{
    // base cases
    if (n == 2) return 1;
    if (n == 4) return 2;
    if (n == 6) return 4;
 
    // initialize variables
    int sum = 0, i, prev2 = 1, prev4 = 2, prev6 = 4;
 
    // iterate for all possible
    // values of i
    for (i = 8; i <= n; i += 2) {
        sum = prev2 + prev4 + prev6;
        prev2 = prev4;
        prev4 = prev6;
        prev6 = sum;
    }
 
    // return the number of ways to reach n
    return sum;
}
 
public static void main(String[] args)
{
    int n = 8;
    System.out.println(count(n));
}
}




# Python program for above approach
 
# Returns number of ways
# to reach score n
def count(n):
    # base cases
    if n == 2:
        return 1
    if n == 4:
        return 2
    if n == 6:
        return 4
 
    # initialize variables
    sum = 0
    prev2 = 1
    prev4 = 2
    prev6 = 4
 
    # iterate for all possible
    # values of i
    for i in range(8, n+1, 2):
        sum = prev2 + prev4 + prev6
        prev2 = prev4
        prev4 = prev6
        prev6 = sum
 
    # return the number of ways to reach n
    return sum
 
if __name__ == '__main__':
    n = 8
    print(count(n))




using System;
 
class Program
{
    // Function to calculate the number of ways to reach score n
    static int Count(int n)
    {
        // Base cases
        if (n == 2) return 1;
        if (n == 4) return 2;
        if (n == 6) return 4;
 
        // Initialize variables
        int sum = 0, i, prev2 = 1, prev4 = 2, prev6 = 4;
 
        // Iterate for all possible values of i
        for (i = 8; i <= n; i += 2)
        {
            // Calculate the current sum based on previous values
            sum = prev2 + prev4 + prev6;
 
            // Update previous values for the next iteration
            prev2 = prev4;
            prev4 = prev6;
            prev6 = sum;
        }
 
        // Return the number of ways to reach n
        return sum;
    }
 
    static void Main()
    {
        int n = 8;
 
        // Call the Count function to calculate and print the result
        int ways = Count(n);
        Console.WriteLine("Number of ways to reach {0}: {1}", n, ways);
    }
}
 
// This code is contributed by Dwaipayan Bandyopadhyay




function count(n) {
    // Base cases
    if (n === 2) return 1;
    if (n === 4) return 2;
    if (n === 6) return 4;
     
    // Initialize variables
    let sum = 0;
    let prev2 = 1;
    let prev4 = 2;
    let prev6 = 4;
     
    // Iterate for all possible
    // values of i
    for (let i = 8; i <= n; i += 2) {
        sum = prev2 + prev4 + prev6;
        prev2 = prev4;
        prev4 = prev6;
        prev6 = sum;
    }
     
    // Return the number of ways
    // to reach n
    return sum;
}
// Driver Code
const n = 8;
console.log(count(n));

Output
7

Time Complexity: O(N) 
Auxiliary Space Complexity: O(1)


Article Tags :