# Find all distinct three digit numbers from given array of digits

Last Updated : 10 Aug, 2022

Given an array containing digits[], where each element is a single digit integer. The array may contain duplicates. The task is to find all the unique integers that follow the given requirements:

• The integer consists of the concatenation of three elements from digits in any arbitrary order.
• The integer does not have leading zeros.

Examples:

Input: digits[] = {2, 1, 3, 0}
Output: {102, 103, 120, 123, 130, 132, 201, 203, 210, 213, 230, 231, 301, 302, 310, 312, 320, 321}
Explanation: The above are the three digit numbers formed.

Input: digits[] = {3, 7, 5}
Output: [357, 375, 537, 573, 735, 753 ]

Approach: This problem can be solved by using Frequency Map. Find count of all elements in given digits array. Follow the steps below to solve the given problem.

• Check for all numbers between 100 to 999 whether they can be formed by the digits present in the digits vector.
• Use 2 maps for the same. If the number can be made, then add it to the answer.
• In the end, return the answer.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;`   `// Function to find all the unique` `// 3 digit number that can be formed` `// from the given digits` `vector<``int``> find3DigitNumbers(vector<``int``>&` `                                  ``digits)` `{` `    ``// Generating frequency map` `    ``// of the given digits` `    ``vector<``int``> count(10, 0);` `    ``for` `(``auto``& d : digits)` `        ``count[d]++;`   `    ``vector<``int``> res;`   `    ``for` `(``int` `num = 100; num < 999; num++) {`   `        ``// Generating frequency map` `        ``// of the current number` `        ``vector<``int``> currCount(10, 0);` `        ``int` `temp = num;`   `        ``while` `(temp) {` `            ``currCount[temp % 10]++;` `            ``temp /= 10;` `        ``}`   `        ``// Checking if the number` `        ``// can be generated or not` `        ``bool` `flag = ``true``;`   `        ``for` `(``int` `i = 0; i < 10; i++) {` `            ``if` `(currCount[i] > count[i]) {` `                ``flag = ``false``;` `                ``break``;` `            ``}` `        ``}`   `        ``if` `(flag) {` `            ``res.push_back(num);` `        ``}` `    ``}` `    ``return` `res;` `}`   `// Function to print answer` `void` `printAnswer(vector<``int``>& v1)` `{` `    ``for` `(``int` `i = 0; i < v1.size(); i++) {` `        ``cout << v1[i] << ``" "``;` `    ``}` `    ``cout << endl;` `}`   `// Driver code` `int` `main()` `{` `    ``vector<``int``> v1 = { 2, 1, 3, 0 };`   `    ``// Function Call` `    ``vector<``int``> ans = find3DigitNumbers(v1);`   `    ``// Printing answer` `    ``printAnswer(ans);` `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.io.*;` `import` `java.lang.*;` `import` `java.util.*;`   `class` `GFG {`   `  ``// Function to find all the unique` `  ``// 3 digit number that can be formed` `  ``// from the given digits` `  ``static` `void` `find3DigitNumbers(``int` `digits[], List res)` `  ``{` `    ``// Generating frequency map` `    ``// of the given digits` `    ``int` `count[] = ``new` `int``[``10``];;` `    ``for` `(``int` `i = ``0``; i < digits.length; i++)` `      ``count[digits[i]]++;`   `    ``for` `(``int` `num = ``100``; num < ``999``; num++) {`   `      ``// Generating frequency map` `      ``// of the current number` `      ``int` `currCount[] = ``new` `int``[``10``];` `      ``int` `temp = num;`   `      ``while` `(temp > ``0``) {` `        ``currCount[temp % ``10``]++;` `        ``temp /= ``10``;` `      ``}`   `      ``// Checking if the number` `      ``// can be generated or not` `      ``Boolean flag = ``true``;`   `      ``for` `(``int` `i = ``0``; i < ``10``; i++) {` `        ``if` `(currCount[i] > count[i]) {` `          ``flag = ``false``;` `          ``break``;` `        ``}` `      ``}`   `      ``if` `(flag == ``true``) {` `        ``res.add(num);` `      ``}` `    ``}` `  ``}`   `  ``// Function to print answer` `  ``static` `void` `printAnswer(List res)` `  ``{` `    ``for``(``int` `i = ``0``;  i < res.size(); i++)` `      ``System.out.print(res.get(i) + ``" "``);` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `main (String[] args) ` `  ``{`   `    ``int` `arr[] = { ``2``, ``1``, ``3``, ``0` `};`   `    ``List ans=``new` `ArrayList(); ` `    ``// Function Call` `    ``find3DigitNumbers(arr, ans);`   `    ``// Printing answer` `    ``printAnswer(ans);` `  ``}` `}`   `// This code is contributed by hrithikgarg03188.`

## Python3

 `# Python code for the above approach`   `# Function to find all the unique` `# 3 digit number that can be formed` `# from the given digits` `def` `find3DigitNumbers(digits):` `  `  `    ``# Generating frequency map` `    ``# of the given digits` `    ``count ``=` `[``0``] ``*` `10` `    ``for` `d ``in` `digits:` `        ``count[d] ``+``=` `1`   `    ``res ``=` `[]`   `    ``for` `num ``in` `range``(``100``, ``999``):`   `        ``# Generating frequency map` `        ``# of the current number` `        ``currCount ``=` `[``0``] ``*` `10` `        ``temp ``=` `num`   `        ``while` `(temp):` `            ``currCount[temp ``%` `10``] ``+``=` `1` `            ``temp ``=` `temp ``/``/` `10`   `        ``# Checking if the number` `        ``# can be generated or not` `        ``flag ``=` `True`   `        ``for` `i ``in` `range``(``10``):` `            ``if` `(currCount[i] > count[i]):` `                ``flag ``=` `False` `                ``break`   `        ``if` `(flag):` `            ``res.append(num)`   `    ``return` `res`   `# Function to print answer` `def` `printAnswer(v1):` `    ``for` `i ``in` `range``(``len``(v1)):` `        ``print``(v1[i], end``=``" "``)`   `    ``print``('')`   `# Driver code` `v1 ``=` `[``2``, ``1``, ``3``, ``0``]`   `# Function Call` `ans ``=` `find3DigitNumbers(v1)`   `# Printing answer` `printAnswer(ans)`     `# This code is contributed by Saurabh Jaiswal`

## C#

 `// C# program for the above approach` `using` `System;` `using` `System.Collections;` `class` `GFG {`   `  ``// Function to find all the unique` `  ``// 3 digit number that can be formed` `  ``// from the given digits` `  ``static` `void` `find3DigitNumbers(``int` `[]digits, ArrayList res)` `  ``{` `    ``// Generating frequency map` `    ``// of the given digits` `    ``int` `[]count = ``new` `int``[10];;` `    ``for` `(``int` `i = 0; i < digits.Length; i++)` `      ``count[digits[i]]++;`   `    ``for` `(``int` `num = 100; num < 999; num++) {`   `      ``// Generating frequency map` `      ``// of the current number` `      ``int` `[]currCount = ``new` `int``[10];` `      ``int` `temp = num;`   `      ``while` `(temp > 0) {` `        ``currCount[temp % 10]++;` `        ``temp /= 10;` `      ``}`   `      ``// Checking if the number` `      ``// can be generated or not` `      ``bool` `flag = ``true``;`   `      ``for` `(``int` `i = 0; i < 10; i++) {` `        ``if` `(currCount[i] > count[i]) {` `          ``flag = ``false``;` `          ``break``;` `        ``}` `      ``}`   `      ``if` `(flag == ``true``) {` `        ``res.Add(num);` `      ``}` `    ``}` `  ``}`   `  ``// Function to print answer` `  ``static` `void` `printAnswer(ArrayList res)` `  ``{` `    ``for``(``int` `i = 0;  i < res.Count; i++)` `      ``Console.Write(res[i] + ``" "``);` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `Main () ` `  ``{`   `    ``int` `[]arr = { 2, 1, 3, 0 };`   `    ``ArrayList ans=``new` `ArrayList(); ` `    ``// Function Call` `    ``find3DigitNumbers(arr, ans);`   `    ``// Printing answer` `    ``printAnswer(ans);` `  ``}` `}`   `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`102 103 120 123 130 132 201 203 210 213 230 231 301 302 310 312 320 321 `

Time Complexity: O(N)
Auxiliary Space: O(N)

Previous
Next