Open In App

Count all N-length arrays made up of distinct consecutive elements whose first and last elements are equal

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 = 3
Output: 6
Explanation:
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 = 4
Output: 84

Approach: Follow the steps below to solve the problem:

 



Below is the implementation of the above approach:

 




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




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




# Python program for the above approach
 
# Function to print the 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;
 
    # Print answer
    print(ans);
     
# This code is contributed by 29AjayKumar




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




<script>
 
// Javascript program for the above approach
 
// Function to print the count of
// arrays satisfying given condition
function totalArrays(N, M)
{
 
    var end_with_one = Array(N+1);
    var end_not_with_one = Array(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 (var 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
 
var N = 3, M = 3;
 
// Stores the count of arrays
// where arr[0] = arr[N - 1] = 1
var temp = totalArrays(N, M);
 
// Since arr[0] and arr[N - 1]
// can be any number from 1 to M
var ans = M * temp;
 
// Print answer
document.write( ans + "<br>");
 
 
</script>

Output: 
6

 

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


Article Tags :