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++
#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
int totalWays( int n, vector< int > capacity)
{
sort(capacity.begin(), capacity.end());
long long ans = capacity[0];
for ( int i = 1; i < n; i++)
ans = (ans * (capacity[i] - i)) % M;
return ans;
}
int main()
{
int N = 2;
vector< int > arr = { 5, 8 };
cout << totalWays(N, arr) << endl;
N = 3;
arr = { 2, 1, 2 };
cout << totalWays(N, arr) << endl;
N = 1;
arr = { 6 };
cout << totalWays(N, arr) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int M = ( int )1e9 + 7 ;
static int totalWays( int n, int [] capacity)
{
Arrays.sort(capacity);
int ans = capacity[ 0 ];
for ( int i = 1 ; i < n; i++) {
ans = (ans * (capacity[i] - i)) % M;
}
return ans;
}
public static void main(String[] args)
{
int N = 2 ;
int [] arr = { 5 , 8 };
System.out.println(totalWays(N, arr));
N = 3 ;
arr = new int [] { 2 , 1 , 2 };
System.out.println(totalWays(N, arr));
N = 1 ;
arr = new int [] { 6 };
System.out.println(totalWays(N, arr));
}
}
|
Python3
M = int ( 1e9 ) + 7 ;
def totalWays(n, capacity) :
capacity.sort();
ans = capacity[ 0 ];
for i in range ( 1 , n) :
ans = (ans * (capacity[i] - i)) % M;
return ans;
if __name__ = = "__main__" :
N = 2 ;
arr = [ 5 , 8 ];
print (totalWays(N, arr));
N = 3 ;
arr = [ 2 , 1 , 2 ];
print (totalWays(N, arr));
N = 1 ;
arr = [ 6 ];
print (totalWays(N, arr));
|
C#
using System;
using System.Collections;
public class GFG {
static int M = ( int )1e9 + 7;
static int totalWays( int n, int [] capacity)
{
Array.Sort(capacity);
int ans = capacity[0];
for ( int i = 1; i < n; i++) {
ans = (ans * (capacity[i] - i)) % M;
}
return ans;
}
static public void Main()
{
int N = 2;
int [] arr = { 5, 8 };
Console.WriteLine(totalWays(N, arr));
N = 3;
arr = new int [] { 2, 1, 2 };
Console.WriteLine(totalWays(N, arr));
N = 1;
arr = new int [] { 6 };
Console.WriteLine(totalWays(N, arr));
}
}
|
Javascript
let M = 1e9 + 7;
function totalWays(n, capacity)
{
capacity.sort()
let ans = capacity[0];
for (let i = 1; i < n; i++)
ans = (ans * (capacity[i] - i)) % M;
return ans;
}
let N = 2;
let arr = [ 5, 8 ];
console.log(totalWays(N, arr));
N = 3;
arr = [ 2, 1, 2 ];
console.log(totalWays(N, arr));
N = 1;
arr = [ 6 ];
console.log(totalWays(N, arr));
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...