📌  相关文章
📜  计算在 M 个连续索引处生成具有不同元素的数组的方法

📅  最后修改于: 2021-10-28 01:32:52             🧑  作者: Mango

给定的阵列ARR []的范围内,包括N个整数的[0,M]的整数M,则任务是计数的方式的数量以取代所有数组元素,其值是0与非零值从区间[ 0, M]使得所有可能的M个连续元素都是不同的。

例子:

方法:想法是用非零元素替换0 s,使得arr[i]必须等于arr[i % M] 。请按照以下步骤解决问题:

  • 初始化大小为M + 1的数组B[] ,以存储M个连续的数组数组元素,使得arr[i]等于B[i % M]
  • 遍历数组并检查以下条件:
    • 如果arr[i]不为0B[i % M]0 ,则B[i % M]将等于arr[i] ,因为该数字应按原样存在。
    • 如果arr[i]不等于B[i % M] ,则打印0因为不存在这样的安排。
  • 计算数组B[]0的计数,比如X
  • 那么,存在阶乘X可能的排列,因此,打印阶乘X的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Modular function
// to calculate factorial
long long int Fact(int N)
{
    // Stores factorial of N
    long long int result = 1;
 
    // Iterate over the range [1, N]
    for (int i = 1; i <= N; i++) {
 
        // Update result
        result = (result * i);
    }
 
    return result;
}
 
// Function to count ways to replace array
// elements having 0s with non-zero elements
// such that any M consective elements are distinct
void numberOfWays(int M, int arr[], int N)
{
 
    // Store m consective distinct elements
    // such that arr[i] is equal to B[i % M]
    int B[M] = { 0 };
 
    // Stores frequency of array elements
    int counter[M + 1] = { 0 };
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // If arr[i] is non-zero
        if (arr[i] != 0) {
 
            // If B[i % M] is equal to 0
            if (B[i % M] == 0) {
 
                // Update B[i % M]
                B[i % M] = arr[i];
 
                // Update frequency of arr[i]
                counter[arr[i]]++;
 
                // If a duplicate element found
                // in M consective elements
                if (counter[arr[i]] > 1) {
                    cout << 0 << endl;
                    return;
                }
            }
 
            // Handling the case of
            // inequality
            else if (B[i % M] != arr[i]) {
 
                cout << 0 << endl;
                return;
            }
        }
    }
 
    // Stores count of 0s
    // in B[]
    int cnt = 0;
 
    // Traverse the array, B[]
    for (int i = 0; i < M; i++) {
 
        // If B[i] is 0
        if (B[i] == 0) {
 
            // Update cnt
            cnt++;
        }
    }
 
    // Calculate factorial
    cout << Fact(cnt) << endl;
}
 
// Driver Code
int main()
{
 
    // Given M
    int M = 4;
 
    // Given array
    int arr[] = { 1, 0, 3, 0, 0 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    numberOfWays(M, arr, N);
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Modular function
// to calculate factorial
static int Fact(int N)
{
     
    // Stores factorial of N
    int result = 1;
 
    // Iterate over the range [1, N]
    for(int i = 1; i <= N; i++)
    {
         
        // Update result
        result = (result * i);
    }
    return result;
}
 
// Function to count ways to replace array
// elements having 0s with non-zero elements
// such that any M consective elements are distinct
static void numberOfWays(int M, int[] arr, int N)
{
     
    // Store m consective distinct elements
    // such that arr[i] is equal to B[i % M]
    int[] B = new int[M];
 
    // Stores frequency of array elements
    int[] counter = new int[M + 1];
 
    // Traverse the array arr[]
    for(int i = 0; i < N; i++)
    {
         
        // If arr[i] is non-zero
        if (arr[i] != 0)
        {
             
            // If B[i % M] is equal to 0
            if (B[i % M] == 0)
            {
                 
                // Update B[i % M]
                B[i % M] = arr[i];
 
                // Update frequency of arr[i]
                counter[arr[i]]++;
 
                // If a duplicate element found
                // in M consective elements
                if (counter[arr[i]] > 1)
                {
                    System.out.println(0);
                    return;
                }
            }
 
            // Handling the case of
            // inequality
            else if (B[i % M] != arr[i])
            {
                System.out.println(0);
                return;
            }
        }
    }
 
    // Stores count of 0s
    // in B[]
    int cnt = 0;
 
    // Traverse the array, B[]
    for(int i = 0; i < M; i++)
    {
         
        // If B[i] is 0
        if (B[i] == 0)
        {
             
            // Update cnt
            cnt++;
        }
    }
 
    // Calculate factorial
    System.out.println(Fact(cnt));
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given M
    int M = 4;
 
    // Given array
    int[] arr = new int[]{ 1, 0, 3, 0, 0 };
 
    // Size of the array
    int N = arr.length;
 
    // Function Call
    numberOfWays(M, arr, N);
}
}
 
// This code is contributed by Dharanendra L V


Python3
# Python3 program for the above approach
 
# Modular function
# to calculate factorial
def Fact(N):
     
    # Stores factorial of N
    result = 1
     
    # Iterate over the range [1, N]
    for i in range(1, N + 1):
         
        # Update result
        result = (result * i)
 
    return result
 
# Function to count ways to replace array
# elements having 0s with non-zero elements
# such that any M consective elements are distinct
def numberOfWays(M, arr, N):
     
    # Store m consective distinct elements
    # such that arr[i] is equal to B[i % M]
    B = [0] * (M)
 
    # Stores frequency of array elements
    counter = [0] * (M + 1)
 
    # Traverse the array arr
    for i in range(0, N):
 
        # If arr[i] is non-zero
        if (arr[i] != 0):
 
            # If B[i % M] is equal to 0
            if (B[i % M] == 0):
 
                # Update B[i % M]
                B[i % M] = arr[i]
 
                # Update frequency of arr[i]
                counter[arr[i]] += 1
 
                # If a duplicate element found
                # in M consective elements
                if (counter[arr[i]] > 1):
                    print(0)
                    return
                 
            # Handling the case of
            # inequality
            elif (B[i % M] != arr[i]):
                print(0)
                return
 
    # Stores count of 0s
    # in B
    cnt = 0
 
    # Traverse the array, B
    for i in range(0, M):
 
        # If B[i] is 0
        if (B[i] == 0):
             
            # Update cnt
            cnt += 1
 
    # Calculate factorial
    print(Fact(cnt))
 
# Driver Code
if __name__ == '__main__':
     
    # Given M
    M = 4
     
    # Given array
    arr = [ 1, 0, 3, 0, 0 ]
 
    # Size of the array
    N = len(arr)
 
    # Function Call
    numberOfWays(M, arr, N)
 
# This code is contributed by shikhasingrajput


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Modular function
// to calculate factorial
static int Fact(int N)
{
     
    // Stores factorial of N
    int result = 1;
 
    // Iterate over the range [1, N]
    for(int i = 1; i <= N; i++)
    {
         
        // Update result
        result = (result * i);
    }
    return result;
}
 
// Function to count ways to replace array
// elements having 0s with non-zero elements
// such that any M consective elements are distinct
static void numberOfWays(int M, int[] arr, int N)
{
     
    // Store m consective distinct elements
    // such that arr[i] is equal to B[i % M]
    int[] B = new int[M];
 
    // Stores frequency of array elements
    int[] counter = new int[M + 1];
 
    // Traverse the array arr[]
    for(int i = 0; i < N; i++)
    {
         
        // If arr[i] is non-zero
        if (arr[i] != 0)
        {
             
            // If B[i % M] is equal to 0
            if (B[i % M] == 0)
            {
                 
                // Update B[i % M]
                B[i % M] = arr[i];
 
                // Update frequency of arr[i]
                counter[arr[i]]++;
 
                // If a duplicate element found
                // in M consective elements
                if (counter[arr[i]] > 1)
                {
                    Console.WriteLine(0);
                    return;
                }
            }
 
            // Handling the case of
            // inequality
            else if (B[i % M] != arr[i])
            {
                Console.WriteLine(0);
                return;
            }
        }
    }
 
    // Stores count of 0s
    // in B[]
    int cnt = 0;
 
    // Traverse the array, B[]
    for(int i = 0; i < M; i++)
    {
         
        // If B[i] is 0
        if (B[i] == 0)
        {
             
            // Update cnt
            cnt++;
        }
    }
 
    // Calculate factorial
    Console.WriteLine(Fact(cnt));
}
 
// Driver Code
static public void Main()
{
     
    // Given M
    int M = 4;
 
    // Given array
    int[] arr = new int[]{ 1, 0, 3, 0, 0 };
 
    // Size of the array
    int N = arr.Length;
 
    // Function Call
    numberOfWays(M, arr, N);
}
}
 
// This code is contributed by Dharanendra L V


Javascript


输出:
2

时间复杂度: O(N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程