Count ways to generate an array having distinct elements at M consecutive indices
Last Updated :
25 Oct, 2021
Given an array arr[] consisting of N integers in the range [0, M] and an integer M, the task is to count the number of ways to replace all array elements whose value is 0 with non-zero values from the range [0, M] such that all possible M consecutive elements are distinct.
Examples:
Input: arr[] = { 1, 0, 3, 0, 0 }, M = 4
Output: 2
Explanation:
Possible ways to replace arr[1], arr[3] and arr[4] with non-zero values such that no M( = 4) consecutive elements contains any duplicate elements are { { 1, 2, 3, 4, 1 }, { 1, 4, 3, 2, 1 } }.
Therefore, the required output is 2.
Input: arr[] = {0, 1, 2, 1, 0}, M = 4
Output: 0
Explanation:No such arrangements possible.
Approach: The idea is to replace 0s with non-zero elements such that arr[i] must be equal to arr[i % M]. Follow the steps below to solve the problem:
- Initialize an array B[] of size M + 1, to store M consecutive array array elements such that arr[i] is equal to B[i % M].
- Traverse the array and check the following conditions:
- If arr[i] is not 0 and B[i % M] is 0, then B[i % M] will be equal to arr[i] as this number should be present as it is.
- If arr[i] is not equal to B[i % M], then print 0 as there exist no such arrangements.
- Calculate the count of 0s in the array B[], say X.
- Then, there exist factorial X possible arrangements, therefore, print the value of factorial X .
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long int Fact( int N)
{
long long int result = 1;
for ( int i = 1; i <= N; i++) {
result = (result * i);
}
return result;
}
void numberOfWays( int M, int arr[], int N)
{
int B[M] = { 0 };
int counter[M + 1] = { 0 };
for ( int i = 0; i < N; i++) {
if (arr[i] != 0) {
if (B[i % M] == 0) {
B[i % M] = arr[i];
counter[arr[i]]++;
if (counter[arr[i]] > 1) {
cout << 0 << endl;
return ;
}
}
else if (B[i % M] != arr[i]) {
cout << 0 << endl;
return ;
}
}
}
int cnt = 0;
for ( int i = 0; i < M; i++) {
if (B[i] == 0) {
cnt++;
}
}
cout << Fact(cnt) << endl;
}
int main()
{
int M = 4;
int arr[] = { 1, 0, 3, 0, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
numberOfWays(M, arr, N);
}
|
Java
import java.io.*;
class GFG{
static int Fact( int N)
{
int result = 1 ;
for ( int i = 1 ; i <= N; i++)
{
result = (result * i);
}
return result;
}
static void numberOfWays( int M, int [] arr, int N)
{
int [] B = new int [M];
int [] counter = new int [M + 1 ];
for ( int i = 0 ; i < N; i++)
{
if (arr[i] != 0 )
{
if (B[i % M] == 0 )
{
B[i % M] = arr[i];
counter[arr[i]]++;
if (counter[arr[i]] > 1 )
{
System.out.println( 0 );
return ;
}
}
else if (B[i % M] != arr[i])
{
System.out.println( 0 );
return ;
}
}
}
int cnt = 0 ;
for ( int i = 0 ; i < M; i++)
{
if (B[i] == 0 )
{
cnt++;
}
}
System.out.println(Fact(cnt));
}
public static void main(String[] args)
{
int M = 4 ;
int [] arr = new int []{ 1 , 0 , 3 , 0 , 0 };
int N = arr.length;
numberOfWays(M, arr, N);
}
}
|
Python3
def Fact(N):
result = 1
for i in range ( 1 , N + 1 ):
result = (result * i)
return result
def numberOfWays(M, arr, N):
B = [ 0 ] * (M)
counter = [ 0 ] * (M + 1 )
for i in range ( 0 , N):
if (arr[i] ! = 0 ):
if (B[i % M] = = 0 ):
B[i % M] = arr[i]
counter[arr[i]] + = 1
if (counter[arr[i]] > 1 ):
print ( 0 )
return
elif (B[i % M] ! = arr[i]):
print ( 0 )
return
cnt = 0
for i in range ( 0 , M):
if (B[i] = = 0 ):
cnt + = 1
print (Fact(cnt))
if __name__ = = '__main__' :
M = 4
arr = [ 1 , 0 , 3 , 0 , 0 ]
N = len (arr)
numberOfWays(M, arr, N)
|
C#
using System;
class GFG{
static int Fact( int N)
{
int result = 1;
for ( int i = 1; i <= N; i++)
{
result = (result * i);
}
return result;
}
static void numberOfWays( int M, int [] arr, int N)
{
int [] B = new int [M];
int [] counter = new int [M + 1];
for ( int i = 0; i < N; i++)
{
if (arr[i] != 0)
{
if (B[i % M] == 0)
{
B[i % M] = arr[i];
counter[arr[i]]++;
if (counter[arr[i]] > 1)
{
Console.WriteLine(0);
return ;
}
}
else if (B[i % M] != arr[i])
{
Console.WriteLine(0);
return ;
}
}
}
int cnt = 0;
for ( int i = 0; i < M; i++)
{
if (B[i] == 0)
{
cnt++;
}
}
Console.WriteLine(Fact(cnt));
}
static public void Main()
{
int M = 4;
int [] arr = new int []{ 1, 0, 3, 0, 0 };
int N = arr.Length;
numberOfWays(M, arr, N);
}
}
|
Javascript
<script>
function Fact(N)
{
let result = 1;
for (let i = 1; i <= N; i++)
{
result = (result * i);
}
return result;
}
function numberOfWays(M, arr, N)
{
let B = new Array(M).fill(0);
let counter = new Array(M+1).fill(0);
for (let i = 0; i < N; i++)
{
if (arr[i] != 0)
{
if (B[i % M] == 0)
{
B[i % M] = arr[i];
counter[arr[i]]++;
if (counter[arr[i]] > 1)
{
document.write(0);
return ;
}
}
else if (B[i % M] != arr[i])
{
document.write(0);
return ;
}
}
}
let cnt = 0;
for (let i = 0; i < M; i++)
{
if (B[i] == 0)
{
cnt++;
}
}
document.write(Fact(cnt));
}
let M = 4;
let arr = [ 1, 0, 3, 0, 0 ];
let N = arr.length;
numberOfWays(M, arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...