The Wayback Machine - https://web.archive.org/web/20210106112717/https://www.geeksforgeeks.org/count-all-n-length-arrays-made-up-of-distinct-consecutive-elements-whose-first-and-last-elements-are-equal/
Related Articles

Related Articles

Count all N-length arrays made up of distinct consecutive elements whose first and last elements are equal
  • Last Updated : 06 Jan, 2021

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:

  • 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 ith index with arr[i] = 1, is same as the number of ways to form the array till the (i – 1)th index with 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 ith index with arr[i] ≠ 1 is as follows:
    • If arr[i – 1]= 1, there are (M – 1) numbers to be placed at the ith index.
    • 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

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Output:

6

Time Complexity: O(N)
Auxiliary Space: 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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :