Count all N-length arrays made up of distinct consecutive elements whose first and last elements are equal
Last Updated :
09 Nov, 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
#include <bits/stdc++.h>
using namespace std;
int totalArrays( int N, int M)
{
int end_with_one[N + 1];
int end_not_with_one[N + 1];
end_with_one[0] = 1;
end_not_with_one[0] = 0;
end_with_one[1] = 0;
end_not_with_one[1] = M - 1;
for ( int i = 2; i < N; i++) {
end_with_one[i]
= end_not_with_one[i - 1];
end_not_with_one[i]
= end_with_one[i - 1] * (M - 1)
+ end_not_with_one[i - 1] * (M - 2);
}
return end_with_one[N - 1];
}
int main()
{
int N = 3, M = 3;
int temp = totalArrays(N, M);
int ans = M * temp;
cout << ans << "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int totalArrays( int N, int M)
{
int []end_with_one = new int [N + 1 ];
int []end_not_with_one = new int [N + 1 ];
end_with_one[ 0 ] = 1 ;
end_not_with_one[ 0 ] = 0 ;
end_with_one[ 1 ] = 0 ;
end_not_with_one[ 1 ] = M - 1 ;
for ( int i = 2 ; i < N; i++)
{
end_with_one[i]
= end_not_with_one[i - 1 ];
end_not_with_one[i]
= end_with_one[i - 1 ] * (M - 1 )
+ end_not_with_one[i - 1 ] * (M - 2 );
}
return end_with_one[N - 1 ];
}
public static void main(String[] args)
{
int N = 3 , M = 3 ;
int temp = totalArrays(N, M);
int ans = M * temp;
System.out.print(ans+ "\n" );
}
}
|
Python3
def totalArrays(N, M):
end_with_one = [ 0 ] * (N + 1 );
end_not_with_one = [ 0 ] * (N + 1 );
end_with_one[ 0 ] = 1 ;
end_not_with_one[ 0 ] = 0 ;
end_with_one[ 1 ] = 0 ;
end_not_with_one[ 1 ] = M - 1 ;
for i in range ( 2 , N):
end_with_one[i] = end_not_with_one[i - 1 ];
end_not_with_one[i] = end_with_one[i - 1 ] * (M - 1 ) + end_not_with_one[i - 1 ] * (M - 2 );
return end_with_one[N - 1 ];
if __name__ = = '__main__' :
N = 3 ;
M = 3 ;
temp = totalArrays(N, M);
ans = M * temp;
print (ans);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int totalArrays( int N, int M)
{
int [] end_with_one = new int [N + 1];
int [] end_not_with_one = new int [N + 1];
end_with_one[0] = 1;
end_not_with_one[0] = 0;
end_with_one[1] = 0;
end_not_with_one[1] = M - 1;
for ( int i = 2; i < N; i++) {
end_with_one[i]
= end_not_with_one[i - 1];
end_not_with_one[i]
= end_with_one[i - 1] * (M - 1)
+ end_not_with_one[i - 1] * (M - 2);
}
return end_with_one[N - 1];
}
static void Main()
{
int N = 3, M = 3;
int temp = totalArrays(N, M);
int ans = M * temp;
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function totalArrays(N, M)
{
var end_with_one = Array(N+1);
var end_not_with_one = Array(N+1);
end_with_one[0] = 1;
end_not_with_one[0] = 0;
end_with_one[1] = 0;
end_not_with_one[1] = M - 1;
for ( var i = 2; i < N; i++) {
end_with_one[i]
= end_not_with_one[i - 1];
end_not_with_one[i]
= end_with_one[i - 1] * (M - 1)
+ end_not_with_one[i - 1] * (M - 2);
}
return end_with_one[N - 1];
}
var N = 3, M = 3;
var temp = totalArrays(N, M);
var ans = M * temp;
document.write( ans + "<br>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...