 Open in App
Not now

# Count ways to fill Array with distinct elements with given limitation

• Last Updated : 01 Dec, 2022

Given an array arr[] of size N and you need to fill another empty array of size N with positive integers. Each element in the given array denotes the maximum positive integer you can choose to fill the empty array. The task is to find the number of ways to fill the empty array such that all elements are distinct.

Examples:

Input: N = 1, arr[] = {6}
Output: 6
Explanation: Since there is only one element. we can fill empty array with values ranging from 1 to 6.

Input: N = 2, arr[] = {5, 8}
Output: 35
Explanation: If the first element is filled with 1 in it then the second element cant have 1 filled in it, so the second element has 8 – 1 = 7 choices. So the first element has 5 choices and for each choice second element has 7 choices.
So total there are 35 ways.

Approach: This problem can be solved using a Greedy Algorithm and Sorting.

The idea is to first greedily choose the smallest capacity number to fill the array so that for the element greater than it the ways become equal to (the greater number – i) where i is the number of element smaller or equal to it. To choose greedily the smallest among the remaining, sort the array at the beginning.

Follow the steps mentioned below to implement the idea:

• Sort the given array.
• Initialize a variable (say ans = 1) to store the answer.
• Initialize the answer as the smallest element
• For every element update answer as ans = ans * (arr[i] – i).

Below is the implementation of the above approach.

## C++

 `// C++ code to implement the approach`   `#include ` `using` `namespace` `std;` `const` `int` `M = 1e9 + 7;`   `// Function to find the total possible ways` `int` `totalWays(``int` `n, vector<``int``> capacity)` `{` `    ``// Sort the given array` `    ``sort(capacity.begin(), capacity.end());`   `    ``// Initialize answer as the` `    ``// smallest element` `    ``long` `long` `ans = capacity;`   `    ``// Now for every element greater than` `    ``// it ways = ways * (capacity[i] - i)` `    ``for` `(``int` `i = 1; i < n; i++)` `        ``ans = (ans * (capacity[i] - i)) % M;` `    ``return` `ans;` `}`   `// Driver code` `int` `main()` `{` `    ``// First test case` `    ``int` `N = 2;` `    ``vector<``int``> arr = { 5, 8 };` `    ``cout << totalWays(N, arr) << endl;`   `    ``// Second test case` `    ``N = 3;` `    ``arr = { 2, 1, 2 };` `    ``cout << totalWays(N, arr) << endl;`   `    ``// Third test case` `    ``N = 1;` `    ``arr = { 6 };` `    ``cout << totalWays(N, arr) << endl;`   `    ``return` `0;` `}`

## Java

 `// Java code to implement the approach`   `import` `java.io.*;` `import` `java.util.*;`   `class` `GFG {`   `    ``static` `int` `M = (``int``)1e9 + ``7``;`   `    ``// Function to find the total possible ways` `    ``static` `int` `totalWays(``int` `n, ``int``[] capacity)` `    ``{` `        ``// sort the given array` `        ``Arrays.sort(capacity);`   `        ``// Initialize answer as the smallest element` `        ``int` `ans = capacity[``0``];`   `        ``// Now for every element greater than it` `        ``// ways = ways * (capacity[i] - i)` `        ``for` `(``int` `i = ``1``; i < n; i++) {` `            ``ans = (ans * (capacity[i] - i)) % M;` `        ``}`   `        ``return` `ans;` `    ``}`   `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``// First test case` `        ``int` `N = ``2``;` `        ``int``[] arr = { ``5``, ``8` `};` `        ``System.out.println(totalWays(N, arr));`   `        ``// Second test case` `        ``N = ``3``;` `        ``arr = ``new` `int``[] { ``2``, ``1``, ``2` `};` `        ``System.out.println(totalWays(N, arr));`   `        ``// Third test case` `        ``N = ``1``;` `        ``arr = ``new` `int``[] { ``6` `};` `        ``System.out.println(totalWays(N, arr));` `    ``}` `}`   `// This code is contributed by lokesh.`

## Python3

 `# Python3 code to implement the above approach` `M ``=` `int``(``1e9``) ``+` `7``;`   `# Function to find the total possible ways` `def` `totalWays(n, capacity) :`   `    ``# Sort the given array` `    ``capacity.sort();`   `    ``# Initialize answer as the` `    ``# smallest element` `    ``ans ``=` `capacity[``0``];`   `    ``# Now for every element greater than` `    ``# it ways = ways * (capacity[i] - i)` `    ``for` `i ``in` `range``(``1``, n) :` `        ``ans ``=` `(ans ``*` `(capacity[i] ``-` `i)) ``%` `M;` `    ``return` `ans;`   `# Driver code` `if` `__name__ ``=``=` `"__main__"` `:`   `    ``# First test case` `    ``N ``=` `2``;` `    ``arr ``=` `[ ``5``, ``8` `];` `    ``print``(totalWays(N, arr));`   `    ``# Second test case` `    ``N ``=` `3``;` `    ``arr ``=` `[ ``2``, ``1``, ``2` `];` `    ``print``(totalWays(N, arr));`   `    ``# Third test case` `    ``N ``=` `1``;` `    ``arr ``=` `[ ``6` `];` `    ``print``(totalWays(N, arr));`   `   ``# This code is contributed by AnkThon`

## C#

 `// C# code to implement the approach`   `using` `System;` `using` `System.Collections;`   `public` `class` `GFG {`   `    ``static` `int` `M = (``int``)1e9 + 7;`   `    ``// Function to find the total possible ways` `    ``static` `int` `totalWays(``int` `n, ``int``[] capacity)` `    ``{` `        ``// sort the given array` `        ``Array.Sort(capacity);`   `        ``// Initialize answer as the smallest element` `        ``int` `ans = capacity;`   `        ``// Now for every element greater than it` `        ``// ways = ways * (capacity[i] - i)` `        ``for` `(``int` `i = 1; i < n; i++) {` `            ``ans = (ans * (capacity[i] - i)) % M;` `        ``}`   `        ``return` `ans;` `    ``}`   `    ``static` `public` `void` `Main()` `    ``{`   `        ``// First test case` `        ``int` `N = 2;` `        ``int``[] arr = { 5, 8 };` `        ``Console.WriteLine(totalWays(N, arr));`   `        ``// Second test case` `        ``N = 3;` `        ``arr = ``new` `int``[] { 2, 1, 2 };` `        ``Console.WriteLine(totalWays(N, arr));`   `        ``// Third test case` `        ``N = 1;` `        ``arr = ``new` `int``[] { 6 };` `        ``Console.WriteLine(totalWays(N, arr));` `    ``}` `}`   `// This code is contributed by lokeshmvs21.`

## Javascript

 `// JavaScript code to implement the approach` `let M = 1e9 + 7;`   `// Function to find the total possible ways` `function` `totalWays(n, capacity)` `{` `    ``// Sort the given array` `    ``capacity.sort()`   `    ``// Initialize answer as the` `    ``// smallest element` `    ``let ans = capacity;`   `    ``// Now for every element greater than` `    ``// it ways = ways * (capacity[i] - i)` `    ``for` `(let i = 1; i < n; i++)` `        ``ans = (ans * (capacity[i] - i)) % M;` `    ``return` `ans;` `}`   `// Driver code` `// First test case` `let N = 2;` `let arr = [ 5, 8 ];` `console.log(totalWays(N, arr));`   `// Second test case` `N = 3;` `arr = [ 2, 1, 2 ];` `console.log(totalWays(N, arr));`   `// Third test case` `N = 1;` `arr = [ 6 ];` `console.log(totalWays(N, arr));`   `// This code is contributed by AnkThon`

Output

```35
0
6```

Time Complexity: O(N * log(N))
Auxiliary Space: O(1)

Related Articles:

My Personal Notes arrow_drop_up
Related Articles