Given two integers **M** and **N**, the task is to find the number of **N**-length arrays possible having non-equal adjacent elements lying in the range **[1, M]** having elements at first and last indices equal.

**Examples:**

Input:N = 3, M = 3Output:6Explanation:The possible arrays are {1, 2, 1}, {1, 3, 1}, {2, 1, 2}, {2, 3, 2}, {3, 1, 3}, {3, 2, 3}.

Input:N = 5, M = 4Output:84

**Approach:** Follow the steps below to solve the problem:

- First fix
**arr[0]**and**arr[N-1]**equal to 1. - Now find the number of arrays possible of size
**i**which ends with**1**(i.e.,*arr[i] = 1*). Store this result into**end_with_one[i]**. - Now, find the number of arrays possible of size
**i**which does not end with**1**(*arr[i] ≠ 1*). Store this result into**end_not_with_one[i]**. - Since, the number of ways to form the array till the
**i**index with^{th}**arr[i] = 1**, is same as the number of ways to form the array till the**(i – 1)**index with^{th}**arr[i – 1] ≠ 1**, set**end_with_one[i] = end_not_with_one[i – 1]**. - Now, the number of ways to form the array till the
**i**index with^{th}**arr[i] ≠ 1**is as follows:**If arr[i – 1]= 1,**there are**(M – 1)**numbers to be placed at the**i**index.^{th}**If arr[i – 1] ≠ 1,**then**(M – 2)**numbers can be placed at index**i**, since**arr[i]**cannot be 1 and**arr[i]**cannot be equal to**arr[i – 1]**.- Therefore, set
**end_not_with_one[i] = end_with_one[i-1] * (M – 1) + end_not_with_one[i-1]* (M – 2)**.

- Therefore, the number of ways to form arrays of size
**N**with**arr[0]**and**arr[N – 1]**equal to**1**is**end_with_one[N – 1]**. - Similarly,
**arr[0]**and**arr[N – 1]**can be set to any element from**1**to**M**. - Therefore, the total number of arrays possible is
**M * end_with_one[N-1]**.

Below is the implementation of the above approach:

## C++14

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to print the count of` `// arrays satisfying given condition` `int` `totalArrays(` `int` `N, ` `int` `M)` `{` ` ` `int` `end_with_one[N + 1];` ` ` `int` `end_not_with_one[N + 1];` ` ` `// First element of` ` ` `// array is set as 1` ` ` `end_with_one[0] = 1;` ` ` `end_not_with_one[0] = 0;` ` ` `// Since the first element` ` ` `// of arr[] is 1, the` ` ` `// second element can't be 1` ` ` `end_with_one[1] = 0;` ` ` `end_not_with_one[1] = M - 1;` ` ` `// Traverse the remaining indices` ` ` `for` `(` `int` `i = 2; i < N; i++) {` ` ` `// If arr[i] = 1` ` ` `end_with_one[i]` ` ` `= end_not_with_one[i - 1];` ` ` `// If arr[i] ≠ 1` ` ` `end_not_with_one[i]` ` ` `= end_with_one[i - 1] * (M - 1)` ` ` `+ end_not_with_one[i - 1] * (M - 2);` ` ` `}` ` ` `// Since last element needs to be 1` ` ` `return` `end_with_one[N - 1];` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 3, M = 3;` ` ` `// Stores the count of arrays` ` ` `// where arr[0] = arr[N - 1] = 1` ` ` `int` `temp = totalArrays(N, M);` ` ` `// Since arr[0] and arr[N - 1]` ` ` `// can be any number from 1 to M` ` ` `int` `ans = M * temp;` ` ` `// Print answer` ` ` `cout << ans << ` `"\n"` `;` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG` `{` `// Function to print the count of` `// arrays satisfying given condition` `static` `int` `totalArrays(` `int` `N, ` `int` `M)` `{` ` ` `int` `[]end_with_one = ` `new` `int` `[N + ` `1` `];` ` ` `int` `[]end_not_with_one = ` `new` `int` `[N + ` `1` `];` ` ` `// First element of` ` ` `// array is set as 1` ` ` `end_with_one[` `0` `] = ` `1` `;` ` ` `end_not_with_one[` `0` `] = ` `0` `;` ` ` `// Since the first element` ` ` `// of arr[] is 1, the` ` ` `// second element can't be 1` ` ` `end_with_one[` `1` `] = ` `0` `;` ` ` `end_not_with_one[` `1` `] = M - ` `1` `;` ` ` `// Traverse the remaining indices` ` ` `for` `(` `int` `i = ` `2` `; i < N; i++)` ` ` `{` ` ` `// If arr[i] = 1` ` ` `end_with_one[i]` ` ` `= end_not_with_one[i - ` `1` `];` ` ` `// If arr[i] ≠ 1` ` ` `end_not_with_one[i]` ` ` `= end_with_one[i - ` `1` `] * (M - ` `1` `)` ` ` `+ end_not_with_one[i - ` `1` `] * (M - ` `2` `);` ` ` `}` ` ` `// Since last element needs to be 1` ` ` `return` `end_with_one[N - ` `1` `];` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `N = ` `3` `, M = ` `3` `;` ` ` `// Stores the count of arrays` ` ` `// where arr[0] = arr[N - 1] = 1` ` ` `int` `temp = totalArrays(N, M);` ` ` `// Since arr[0] and arr[N - 1]` ` ` `// can be any number from 1 to M` ` ` `int` `ans = M * temp;` ` ` `// Print answer` ` ` `System.out.print(ans+ ` `"\n"` `);` `}` `}` `// This code is contributed by 29AjayKumar` |

*chevron_right*

*filter_none*

## Python3

`# Python program for the above approach` `# Function to prthe count of` `# arrays satisfying given condition` `def` `totalArrays(N, M):` ` ` `end_with_one ` `=` `[` `0` `] ` `*` `(N ` `+` `1` `);` ` ` `end_not_with_one ` `=` `[` `0` `] ` `*` `(N ` `+` `1` `);` ` ` `# First element of` ` ` `# array is set as 1` ` ` `end_with_one[` `0` `] ` `=` `1` `;` ` ` `end_not_with_one[` `0` `] ` `=` `0` `;` ` ` `# Since the first element` ` ` `# of arr is 1, the` ` ` `# second element can't be 1` ` ` `end_with_one[` `1` `] ` `=` `0` `;` ` ` `end_not_with_one[` `1` `] ` `=` `M ` `-` `1` `;` ` ` `# Traverse the remaining indices` ` ` `for` `i ` `in` `range` `(` `2` `, N):` ` ` ` ` `# If arr[i] = 1` ` ` `end_with_one[i] ` `=` `end_not_with_one[i ` `-` `1` `];` ` ` `# If arr[i] ≠ 1` ` ` `end_not_with_one[i] ` `=` `end_with_one[i ` `-` `1` `] ` `*` `(M ` `-` `1` `) ` `+` `end_not_with_one[i ` `-` `1` `] ` `*` `(M ` `-` `2` `);` ` ` `# Since last element needs to be 1` ` ` `return` `end_with_one[N ` `-` `1` `];` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `N ` `=` `3` `;` ` ` `M ` `=` `3` `;` ` ` `# Stores the count of arrays` ` ` `# where arr[0] = arr[N - 1] = 1` ` ` `temp ` `=` `totalArrays(N, M);` ` ` `# Since arr[0] and arr[N - 1]` ` ` `# can be any number from 1 to M` ` ` `ans ` `=` `M ` `*` `temp;` ` ` `# Pranswer` ` ` `print` `(ans);` ` ` `# This code is contributed by 29AjayKumar` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG ` `{` ` ` ` ` `// Function to print the count of ` ` ` `// arrays satisfying given condition ` ` ` `static` `int` `totalArrays(` `int` `N, ` `int` `M) ` ` ` `{ ` ` ` ` ` `int` `[] end_with_one = ` `new` `int` `[N + 1]; ` ` ` `int` `[] end_not_with_one = ` `new` `int` `[N + 1]; ` ` ` ` ` `// First element of ` ` ` `// array is set as 1 ` ` ` `end_with_one[0] = 1; ` ` ` `end_not_with_one[0] = 0; ` ` ` ` ` `// Since the first element ` ` ` `// of arr[] is 1, the ` ` ` `// second element can't be 1 ` ` ` `end_with_one[1] = 0; ` ` ` `end_not_with_one[1] = M - 1; ` ` ` ` ` `// Traverse the remaining indices ` ` ` `for` `(` `int` `i = 2; i < N; i++) { ` ` ` ` ` `// If arr[i] = 1 ` ` ` `end_with_one[i] ` ` ` `= end_not_with_one[i - 1]; ` ` ` ` ` `// If arr[i] ≠ 1 ` ` ` `end_not_with_one[i] ` ` ` `= end_with_one[i - 1] * (M - 1) ` ` ` `+ end_not_with_one[i - 1] * (M - 2); ` ` ` `} ` ` ` ` ` `// Since last element needs to be 1 ` ` ` `return` `end_with_one[N - 1]; ` ` ` `} ` ` ` `// Driver code` ` ` `static` `void` `Main()` ` ` `{` ` ` ` ` `int` `N = 3, M = 3; ` ` ` ` ` `// Stores the count of arrays ` ` ` `// where arr[0] = arr[N - 1] = 1 ` ` ` `int` `temp = totalArrays(N, M); ` ` ` ` ` `// Since arr[0] and arr[N - 1] ` ` ` `// can be any number from 1 to M ` ` ` `int` `ans = M * temp; ` ` ` ` ` `// Print answer ` ` ` `Console.WriteLine(ans); ` ` ` `}` `}` `// This code is contributed by divyeshrabadiya07` |

*chevron_right*

*filter_none*

**Output:**

6

* Time Complexity:* O(N)

*O(N)*

**Auxiliary Space:**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.