📌  相关文章
📜  所有可能数组的计数,使得每个数组元素都可以超出范围 [1, arr[i]]

📅  最后修改于: 2022-05-13 01:56:08.824000             🧑  作者: Mango

所有可能数组的计数,使得每个数组元素都可以超出范围 [1, arr[i]]

给定一个由N个正整数组成的数组arr[] ,任务是找到所有可能的数组的数量,使得每个数组元素都可以超过范围[1, arr[i]]新构造的数组中的所有元素必须是成对不同。

例子:

方法:可以基于观察到数组元素arr[]的顺序与使用新标准生成数组无关,从而解决给定的问题。下面是相同的插图:

插图:

请按照以下步骤解决给定的问题:

  • 以非递减顺序对数组arr[]进行排序。
  • 初始化一个变量,比如res1 ,它存储所有可能形成的数组的计数。
  • 遍历给定的数组arr[]并对每个数组元素arr[i]执行以下步骤:
    • 在索引i处插入新数组元素的所有可能选择的计数是arr[i] ,但要使数组成对不同,不能再次选择所有先前选择的数字。因此,可用选项的总数为(arr[i] – i)
    • 现在,在索引i之前可能的组合总数由res*(arr[i] – i)给出。因此,将 res 的值更新为res *(arr[i] – i)
  • 完成上述步骤后,打印res的值作为可能形成的数组的计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the total number of
// arrays of pairwise distinct element
// and each element lies in [1, arr[i]]
int findArraysInRange(int arr[], int N)
{
    // Stores all possible arrays formed
    int res = 1;
 
    // Sort the array arr[] in the
    // non-decreasing order
    sort(arr, arr + N);
 
    for (int i = 0; i < N; i++) {
 
        // Total combinations for the
        // current index i
        int combinations = (arr[i] - i);
 
        // Update the value of res
        res *= combinations;
    }
 
    // Return the possible count
    return res;
}
 
// Driver Code
int main()
{
    int arr[] = { 4, 4, 4, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << findArraysInRange(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
    // Function to find the total number of
    // arrays of pairwise distinct element
    // and each element lies in [1, arr[i]]
    static int findArraysInRange(int[] arr, int N)
    {
       
        // Stores all possible arrays formed
        int res = 1;
 
        // Sort the array arr[] in the
        // non-decreasing order
        Arrays.sort(arr);
 
        for (int i = 0; i < N; i++) {
 
            // Total combinations for the
            // current index i
            int combinations = (arr[i] - i);
 
            // Update the value of res
            res *= combinations;
        }
 
        // Return the possible count
        return res;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 4, 4, 4, 4 };
        int N = arr.length;
        System.out.print(findArraysInRange(arr, N));
    }
}
 
// This code is contributed by subhammahato348.


Python3
# Python program for the above approach
 
# Function to find the total number of
# arrays of pairwise distinct element
# and each element lies in [1, arr[i]]
def findArraysInRange(arr, n):
   
    # Sort the array
    arr.sort()
 
    # res Stores all possible arrays formed
    res = 1
    i = 0
 
    # Sort the array arr[] in the
    # non-decreasing order
    arr.sort()
 
    for i in range(0, n):
 
        # Total combinations for the
        # current index i
        combinations = (arr[i] - i)
 
        # Update the value of res
        res = res*combinations
 
    # Return the possible count
    return res
 
# Driver Code
arr = [4, 4, 4, 4]
N = len(arr)
print(findArraysInRange(arr, N))
 
# This code is contributed by _saurabh_jaiswal


C#
// C# program for the approach
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Function to find the total number of
    // arrays of pairwise distinct element
    // and each element lies in [1, arr[i]]
    static int findArraysInRange(int[] arr, int N)
    {
        
        // Stores all possible arrays formed
        int res = 1;
  
        // Sort the array arr[] in the
        // non-decreasing order
        Array.Sort(arr);
  
        for (int i = 0; i < N; i++) {
  
            // Total combinations for the
            // current index i
            int combinations = (arr[i] - i);
  
            // Update the value of res
            res *= combinations;
        }
  
        // Return the possible count
        return res;
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 4, 4, 4, 4 };
        int N = arr.Length;
        Console.Write(findArraysInRange(arr, N));
    }
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
24

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