Given three positive integers **n**, **k** and **x**. The task is to count the number of different array that can be formed of size n such that each element is between 1 to k and two consecutive element are different. Also, the first and last elements of each array should be 1 and x respectively.

**Examples :**

Input : n = 4, k = 3, x = 2 Output : 3

The idea is to use Dynamic Programming and combinatorics to solve the problem.

First of all, notice that the answer is same for all x from 2 to k. It can easily be proved. This will be useful later on.

Let the state f(i) denote the number of ways to fill the range [1, i] of array A such that A_{1} = 1 and A_{i} ≠ 1.

Therefore, if x ≠ 1, the answer to the problem is f(n)/(k – 1), because f(n) is the number of way where A_{n} is filled with a number from 2 to k, and the answer are equal for all such values A_{n}, so the answer for an individual value is f(n)/(k – 1).

Otherwise, if x = 1, the answer is f(n – 1), because A_{n – 1} ≠ 1, and the only number we can fill A_{n} with is x = 1.

Now, the main problem is how to calculate f(i). Consider all numbers that A_{i – 1} can be. We know that it must lie in [1, k].

- If A
_{i – 1}≠ 1, then there are (k – 2)f(i – 1) ways to fill in the rest of the array, because A_{i}cannot be 1 or A_{i – 1}(so we multiply with (k – 2)), and for the range [1, i – 1], there are, recursively, f(i – 1) ways. - If A
_{i – 1}= 1, then there are (k – 1)f(i – 2) ways to fill in the rest of the array, because A_{i – 1}= 1 means A_{i – 2}≠ 1 which means there are f(i – 2)ways to fill in the range [1, i – 2] and the only value that A_{i}cannot be 1, so we have (k – 1) choices for A_{i}.

By combining the above, we get

f(i) = (k - 1) * f(i - 2) + (k - 2) * f(i - 1)

This will help us to use dynamic programming using f(i).

Below is the implementation of this approach:

## C++

`// CPP Program to find count of arrays. ` `#include <bits/stdc++.h> ` `#define MAXN 109 ` `using` `namespace` `std; ` ` ` `// Return the number of arrays with given constartints. ` `int` `countarray(` `int` `n, ` `int` `k, ` `int` `x) ` `{ ` ` ` `int` `dp[MAXN] = { 0 }; ` ` ` ` ` `// Initalising dp[0] and dp[1]. ` ` ` `dp[0] = 0; ` ` ` `dp[1] = 1; ` ` ` ` ` `// Computing f(i) for each 2 <= i <= n. ` ` ` `for` `(` `int` `i = 2; i < n; i++) ` ` ` `dp[i] = (k - 2) * dp[i - 1] + ` ` ` `(k - 1) * dp[i - 2]; ` ` ` ` ` `return` `(x == 1 ? (k - 1) * dp[n - 2] : dp[n - 1]); ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `int` `n = 4, k = 3, x = 2; ` ` ` `cout << countarray(n, k, x) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find count of arrays. ` `import` `java.util.*; ` ` ` `class` `Counting ` `{ ` ` ` `static` `int` `MAXN = ` `109` `; ` ` ` ` ` `public` `static` `int` `countarray(` `int` `n, ` `int` `k, ` ` ` `int` `x) ` ` ` `{ ` ` ` `int` `[] dp = ` `new` `int` `[` `109` `]; ` ` ` ` ` `// Initalising dp[0] and dp[1]. ` ` ` `dp[` `0` `] = ` `0` `; ` ` ` `dp[` `1` `] = ` `1` `; ` ` ` ` ` `// Computing f(i) for each 2 <= i <= n. ` ` ` `for` `(` `int` `i = ` `2` `; i < n; i++) ` ` ` `dp[i] = (k - ` `2` `) * dp[i - ` `1` `] + ` ` ` `(k - ` `1` `) * dp[i - ` `2` `]; ` ` ` ` ` `return` `(x == ` `1` `? (k - ` `1` `) * dp[n - ` `2` `] : ` ` ` `dp[n - ` `1` `]); ` ` ` `} ` ` ` ` ` `// driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `4` `, k = ` `3` `, x = ` `2` `; ` ` ` `System.out.println(countarray(n, k, x)); ` ` ` `} ` `} ` ` ` ` ` `// This code is contributed by rishabh_jain ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to find count of arrays. ` ` ` `# Return the number of lists with ` `# given constraints. ` `def` `countarray( n , k , x ): ` ` ` ` ` `dp ` `=` `list` `() ` ` ` ` ` `# Initalising dp[0] and dp[1] ` ` ` `dp.append(` `0` `) ` ` ` `dp.append(` `1` `) ` ` ` ` ` `# Computing f(i) for each 2 <= i <= n. ` ` ` `i ` `=` `2` ` ` `while` `i < n: ` ` ` `dp.append( (k ` `-` `2` `) ` `*` `dp[i ` `-` `1` `] ` `+` ` ` `(k ` `-` `1` `) ` `*` `dp[i ` `-` `2` `]) ` ` ` `i ` `=` `i ` `+` `1` ` ` ` ` `return` `( (k ` `-` `1` `) ` `*` `dp[n ` `-` `2` `] ` `if` `x ` `=` `=` `1` `else` `dp[n ` `-` `1` `]) ` ` ` `# Driven code ` `n ` `=` `4` `k ` `=` `3` `x ` `=` `2` `print` `(countarray(n, k, x)) ` ` ` `# This code is contributed by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find count of arrays. ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// static int MAXN = 109; ` ` ` ` ` `public` `static` `int` `countarray(` `int` `n, ` `int` `k, ` ` ` `int` `x) ` ` ` `{ ` ` ` `int` `[] dp = ` `new` `int` `[109]; ` ` ` ` ` `// Initalising dp[0] and dp[1]. ` ` ` `dp[0] = 0; ` ` ` `dp[1] = 1; ` ` ` ` ` `// Computing f(i) for each 2 <= i <= n. ` ` ` `for` `(` `int` `i = 2; i < n; i++) ` ` ` `dp[i] = (k - 2) * dp[i - 1] + ` ` ` `(k - 1) * dp[i - 2]; ` ` ` ` ` `return` `(x == 1 ? (k - 1) * dp[n - 2] : ` ` ` `dp[n - 1]); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 4, k = 3, x = 2; ` ` ` `Console.WriteLine(countarray(n, k, x)); ` ` ` `} ` `} ` ` ` ` ` `// This code is contributed by vt_m ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to find ` `// count of arrays. ` ` ` `$MAXN` `= 109; ` ` ` `// Return the number of arrays ` `// with given constartints. ` `function` `countarray(` `$n` `, ` `$k` `, ` `$x` `) ` `{ ` ` ` `$dp` `= ` `array` `( 0 ); ` ` ` ` ` `// Initalising dp[0] and dp[1]. ` ` ` `$dp` `[0] = 0; ` ` ` `$dp` `[1] = 1; ` ` ` ` ` `// Computing f(i) for ` ` ` `// each 2 <= i <= n. ` ` ` `for` `( ` `$i` `= 2; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `$dp` `[` `$i` `] = (` `$k` `- 2) * ` `$dp` `[` `$i` `- 1] + ` ` ` `(` `$k` `- 1) * ` `$dp` `[` `$i` `- 2]; ` ` ` ` ` `return` `(` `$x` `== 1 ? (` `$k` `- 1) * ` ` ` `$dp` `[` `$n` `- 2] : ` `$dp` `[` `$n` `- 1]); ` `} ` ` ` `// Driven Code ` `$n` `= 4; ` `$k` `= 3; ` `$x` `= 2; ` `echo` `countarray(` `$n` `, ` `$k` `, ` `$x` `) ; ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

3

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 possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Count of N digit Numbers having no pair of equal consecutive Digits
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of all subsequences having adjacent elements with different parity
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count of possible arrays from prefix-sum and suffix-sum arrays
- Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays
- Count of binary strings of length N having equal count of 0's and 1's
- Maximum sub-matrix area having count of 1's one more than count of 0's
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Number of sub-sequence such that it has one consecutive element with difference less than or equal to 1
- Count Pairs Of Consecutive Zeros
- Count ways to reach a score using 1 and 2 with no consecutive 2s
- Count of N-digit numbers in base K with no two consecutive zeroes
- Count of strings possible by replacing two consecutive same character with new character
- Count number of binary strings without consecutive 1’s : Set 2
- Count the number of ordered sets not containing consecutive numbers

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.