Number of ways to form an array with distinct adjacent elements

Given three integers N, M and X, the task is to find the number of ways to form an array, such that all consecutive numbers of the array are distinct, and the value at any index of the array from 2 to N – 1(Considering 1 based indexing) lies between 1 and M, while the value at index 1 is X and the value at index N is 1.
Note: Value of X lies between 1 and M.

Examples:

Input: N = 4, M = 3, X = 2
Output: 3
The following arrays are possible:
1) 2, 1, 2, 1
2) 2, 1, 3, 1
3) 2, 3, 2, 1

Input: N = 2, M = 3, X = 2
Output: 1
The only possible array is: 2, 1



Approach: The problem can be solved using Dynamic Programming. Let, f(i) represent the number of ways to form the array till the ith index, such that every consecutive element of the array is distinct. Let f(i, One) represent the number of ways to form the array till the i-th index such that every consecutive element of the array is distinct and arri = 1.
Similarly, let f(i, Non-One) represent the number of ways to form the array till the ith index, such that every consecutive element of the array is distinct and arri is not equal to 1.

The following recurrence is formed:

f(i, Non-One) = f(i - 1, One) * (M - 1) + f(i - 1, Non-One) * (M - 2)

which means that the number of ways to form the array till the ith index with arrayi not equal to 1 is formed using two cases:

  1. If the number at arrayi – 1 is 1, then opt one number out of (M – 1) options to place at the ith index, since arrayi is not equal to 1.
  2. If the number at arrayi – 1 is not 1, then we need to opt one number out of (M – 2) options, since arrayi is not equal to 1 and arrayi ≠ arrayi – 1.

Similarly, f(i, One) = f(i – 1, Non-One), since the number of ways to form the array till the ith index with arrayi = 1, is same as number of ways to form the array till the (i – 1)th index with arrayi – 1 ≠ 1, thus at the ith index there is only one option. At the end the required answer if f(N, One) since arrayN needs to be equal to 1.

Below is the implementation of the above approach:

C++

// C++ program to count the number of ways
// to form arrays of N numbers such that the
// first and last numbers are fixed
// and all consecutive numbers are distinct
#include <bits/stdc++.h>
using namespace std;
  
// Returns the total ways to form arrays such that
// every consecutive element is different and each
// element except the first and last can take values
// from 1 to M
int totalWays(int N, int M, int X)
{
  
    // define the dp[][] array
    int dp[N + 1][2];
  
    // if the first element is 1
    if (X == 1) {
  
        // there is only one way to place
        // a 1 at the first index
        dp[0][0] = 1;
    }
    else {
  
        // the value at first index needs to be 1,
        // thus there is no way to place a non-one integer
        dp[0][1] = 0;
    }
  
    // if the first element was 1 then at index 1,
    // only non one integer can be placed thus
    // there are M - 1 ways to place a non one integer
    // at index 2 and 0 ways to place a 1 at the 2nd index
    if (X == 1) {
        dp[1][0] = 0;
        dp[1][1] = M - 1;
    }
  
    // Else there is one way to place a one at
    // index 2 and if a non one needs to be placed here,
    // there are (M - 2) options, i.e
    // neither the element at this index
    // should be 1, neither should it be
    // equal to the previous element
    else {
        dp[1][0] = 1;
        dp[1][1] = (M - 2);
    }
  
    // Build the dp array in bottom up manner
    for (int i = 2; i < N; i++) {
  
        // f(i, one) = f(i - 1, non-one)
        dp[i][0] = dp[i - 1][1];
  
        // f(i, non-one) = f(i - 1, one) * (M - 1) +
        // f(i - 1, non-one) * (M - 2)
        dp[i][1] = dp[i - 1][0] * (M - 1) + dp[i - 1][1] * (M - 2);
    }
  
    // last element needs to be one, so return dp[n - 1][0]
    return dp[N - 1][0];
}
  
// Driver Code
int main()
{
  
    int N = 4, M = 3, X = 2;
    cout << totalWays(N, M, X) << endl;
  
    return 0;
}

Java

// Java program to count the 
// number of ways to form 
// arrays of N numbers such 
// that the first and last 
// numbers are fixed and all 
// consecutive numbers are 
// distinct
import java.io.*;
  
class GFG 
{
  
// Returns the total ways to 
// form arrays such that every 
// consecutive element is 
// different and each element 
// except the first and last 
// can take values from 1 to M
static int totalWays(int N, 
                     int M, int X)
{
  
    // define the dp[][] array
    int dp[][] = new int[N + 1][2];
  
    // if the first element is 1
    if (X == 1
    {
  
        // there is only one 
        // way to place a 1 
        // at the first index
        dp[0][0] = 1;
    }
    else 
    {
  
        // the value at first index 
        // needs to be 1, thus there 
        // is no way to place a 
        // non-one integer
        dp[0][1] = 0;
    }
  
    // if the first element was 1 
    // then at index 1, only non 
    // one integer can be placed 
    // thus there are M - 1 ways 
    // to place a non one integer
    // at index 2 and 0 ways to 
    // place a 1 at the 2nd index
    if (X == 1)
    {
        dp[1][0] = 0;
        dp[1][1] = M - 1;
    }
  
    // Else there is one way to 
    // place a one at index 2 
    // and if a non one needs to 
    // be placed here, there are 
    // (M - 2) options, i.e neither
    // the element at this index
    // should be 1, neither should 
    // it be equal to the previous 
    // element
    else 
    {
        dp[1][0] = 1;
        dp[1][1] = (M - 2);
    }
  
    // Build the dp array
    // in bottom up manner
    for (int i = 2; i < N; i++) 
    {
  
        // f(i, one) = f(i - 1, 
        // non-one)
        dp[i][0] = dp[i - 1][1];
  
        // f(i, non-one) = 
        // f(i - 1, one) * (M - 1) +
        // f(i - 1, non-one) * (M - 2)
        dp[i][1] = dp[i - 1][0] * (M - 1) + 
                   dp[i - 1][1] * (M - 2);
    }
  
    // last element needs to be
    // one, so return dp[n - 1][0]
    return dp[N - 1][0];
}
  
// Driver Code
public static void main (String[] args)
{
    int N = 4, M = 3, X = 2;
    System.out.println(totalWays(N, M, X));
}
}
  
// This code is contributed by anuj_67.

C#

// C# program to count the 
// number of ways to form 
// arrays of N numbers such 
// that the first and last 
// numbers are fixed and all 
// consecutive numbers are 
// distinct
using System;
  
class GFG 
{
  
// Returns the total ways to 
// form arrays such that every 
// consecutive element is 
// different and each element 
// except the first and last 
// can take values from 1 to M
static int totalWays(int N, 
                     int M, int X)
{
  
    // define the dp[][] array
    int [,]dp = new int[N + 1, 2];
  
    // if the first element is 1
    if (X == 1) 
    {
  
        // there is only one 
        // way to place a 1 
        // at the first index
        dp[0, 0] = 1;
    }
    else
    {
  
        // the value at first index 
        // needs to be 1, thus there 
        // is no way to place a 
        // non-one integer
        dp[0, 1] = 0;
    }
  
    // if the first element was 1 
    // then at index 1, only non 
    // one integer can be placed 
    // thus there are M - 1 ways 
    // to place a non one integer
    // at index 2 and 0 ways to 
    // place a 1 at the 2nd index
    if (X == 1)
    {
        dp[1, 0] = 0;
        dp[1, 1] = M - 1;
    }
  
    // Else there is one way to 
    // place a one at index 2 
    // and if a non one needs to 
    // be placed here, there are 
    // (M - 2) options, i.e neither
    // the element at this index
    // should be 1, neither should 
    // it be equal to the previous 
    // element
    else
    {
        dp[1, 0] = 1;
        dp[1, 1] = (M - 2);
    }
  
    // Build the dp array
    // in bottom up manner
    for (int i = 2; i < N; i++) 
    {
  
        // f(i, one) = f(i - 1, 
        // non-one)
        dp[i, 0] = dp[i - 1, 1];
  
        // f(i, non-one) = 
        // f(i - 1, one) * (M - 1) +
        // f(i - 1, non-one) * (M - 2)
        dp[i, 1] = dp[i - 1, 0] * (M - 1) + 
                   dp[i - 1, 1] * (M - 2);
    }
  
    // last element needs to be
    // one, so return dp[n - 1][0]
    return dp[N - 1, 0];
}
  
// Driver Code
public static void Main ()
{
    int N = 4, M = 3, X = 2;
    Console.WriteLine(totalWays(N, M, X));
}
}
  
// This code is contributed
// by anuj_67.

PHP

<?php
// PHP program to count the 
// number of ways to form 
// arrays of N numbers such 
// that the first and last 
// numbers are fixed and all 
// consecutive numbers are distinct
  
// Returns the total ways to 
// form arrays such that every
// consecutive element is 
// different and each element 
// except the first and last 
// can take values from 1 to M
function totalWays($N, $M, $X)
{
  
    // define the dp[][] array
    $dp = array(array());
  
    // if the first element is 1
    if ($X == 1) 
    {
  
        // there is only one 
        // way to place a 1
        // at the first index
        $dp[0][0] = 1;
    }
    else 
    {
  
        // the value at first 
        // index needs to be 1,
        // thus there is no way 
        // to place a non-one integer
        $dp[0][1] = 0;
    }
  
    // if the first element was 
    // 1 then at index 1, only 
    // non one integer can be 
    // placed thus there are M - 1 
    // ways to place a non one 
    // integer at index 2 and 0 ways
    // to place a 1 at the 2nd index
    if ($X == 1) 
    {
        $dp[1][0] = 0;
        $dp[1][1] = $M - 1;
    }
  
    // Else there is one way 
    // to place a one at index 
    // 2 and if a non one needs 
    // to be placed here, there 
    // are (M - 2) options, i.e
    // neither the element at 
    // this index should be 1,
    // neither should it be equal 
    // to the previous element
    else 
    {
        $dp[1][0] = 1;
        $dp[1][1] = ($M - 2);
    }
  
    // Build the dp array
    // in bottom up manner
    for ($i = 2; $i <$N; $i++) 
    {
  
        // f(i, one) = 
        // f(i - 1, non-one)
        $dp[$i][0] = $dp[$i - 1][1];
  
        // f(i, non-one) = 
        // f(i - 1, one) * (M - 1) +
        // f(i - 1, non-one) * (M - 2)
        $dp[$i][1] = $dp[$i - 1][0] * 
                           ($M - 1) + 
                     $dp[$i - 1][1] * 
                           ($M - 2);
    }
  
    // last element needs to 
    // be one, so return dp[n - 1][0]
    return $dp[$N - 1][0];
}
  
// Driver Code
$N = 4; $M = 3; $X = 2;
echo totalWays($N, $M, $X);
  
// This code is contributed
// by anuj_67.
?>

Output:

3

Time Complexity: O(N), where N is the size of the array



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : vt_m