Given an integer **N**, the task is to find out the number of permutations in which N can be represented as a sum of **2**s, **4**s, and **6**s only.

**Note:** The different permutations of the same combination will also be counted.

**Examples:**

Input:N = 8

Output:7

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:4

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

- As the possible numbers that can be added to reach
**N**are 2, 4, and 6, they can be considered as base cases. By making use of the base cases further cases can be computed. - Let us consider a
**dp[]**array of size**N + 1**which computes and stores at every index**i**the possible ways to form a value**i**using only 2, 4, 6. -
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]** - Hence, dp[N] contains the required answer.

Below is the implementation of the above approach:

## C++

`// 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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// 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 ` |

*chevron_right*

*filter_none*

## Python3

`# 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 ` |

*chevron_right*

*filter_none*

## C#

`// 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 ` |

*chevron_right*

*filter_none*

**Output:**

7

**Time Complexity:*** O(N)*

**Auxillary Space Complexity:** *O(N)*

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count the number of special permutations
- Count numbers which can be represented as sum of same parity primes
- Count permutations that are first decreasing then increasing.
- Count of common subarrays in two different permutations of 1 to N
- Count permutations that produce positive result
- Count of K-size substrings having palindromic permutations
- Count elements in an Array that can be represented as difference of two perfect squares
- Maximize count of corresponding same elements in given permutations using cyclic rotations
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Check whether a number can be represented by sum of two squares
- Check if a number can be represented as sum of non zero powers of 2
- Number of palindromic permutations | Set 1
- Check whether a number can be represented as difference of two squares
- Divide large number represented as string
- Check if a given number can be represented in given a no. of digits in any base
- Number of ways in which N can be represented as the sum of two positive integers
- Check if given number can be represented as sum of two great numbers
- Sum of two numbers where one number is represented as array of digits
- Check if a number can be represented as a sum of 2 triangular numbers
- Check whether a number can be represented by the product of two squares

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.