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 <bits/stdc++.h>
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<Integer> 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<Integer> 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<Integer> ans=new ArrayList<Integer>();
// 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
<script>
// JavaScript code for the above approach
// Function to find all the unique
// 3 digit number that can be formed
// from the given digits
function find3DigitNumbers(
digits) {
// Generating frequency map
// of the given digits
let count = new Array(10).fill(0);
for (let d of digits)
count[d]++;
let res = [];
for (let num = 100; num < 999; num++) {
// Generating frequency map
// of the current number
let currCount = new Array(10).fill(0);
let temp = num;
while (temp) {
currCount[temp % 10]++;
temp = Math.floor(temp / 10);
}
// Checking if the number
// can be generated or not
let flag = true;
for (let i = 0; i < 10; i++) {
if (currCount[i] > count[i]) {
flag = false;
break;
}
}
if (flag) {
res.push(num);
}
}
return res;
}
// Function to print answer
function printAnswer(v1) {
for (let i = 0; i < v1.length; i++) {
document.write(v1[i] + " ");
}
document.write('<br>')
}
// Driver code
let v1 = [2, 1, 3, 0];
// Function Call
let ans = find3DigitNumbers(v1);
// Printing answer
printAnswer(ans);
// This code is contributed by Potta Lokesh
</script>
Output102 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)
Similar Reads
Count of N-digit numbers with all distinct digits Given an integer N, the task is to find the count of N-digit numbers with all distinct digits.Examples: Input: N = 1 Output: 9 1, 2, 3, 4, 5, 6, 7, 8 and 9 are the 1-digit numbers with all distinct digits.Input: N = 3 Output: 648 Naive Approach: If N > 10 i.e. there will be atleast one digit whic
7 min read
Find Next number having distinct digits from the given number N Given a natural number N, the task is to find the next number having distinct digits from the given number.Examples: Input: N = 19 Output: 20 Explanation: Next number to 19 whose digits are different from 19 is 20.Input: N = 2019 Output: 3333 Explanation: Next number to 2019 whose digits are differe
10 min read
Count of distinct numbers formed by shuffling the digits of a large number N Given a large number N in the form of a string, the task is to determine the count of distinct numbers that can be formed by shuffling the digits of the number N. Note: N may contain leading zeros. The number itself is also taken into count.Since the answer could be very large, print result modulo 1
11 min read
Count array elements whose all distinct digits appear in K Given an array arr[] consisting of N positive integers and a positive integer K, the task is to find the count of array elements whose distinct digits are a subset of the digits of K. Examples: Input: arr[] = { 1, 12, 1222, 13, 2 }, K = 12Output: 4Explanation: Distinct Digits of K are { 1, 2 } Disti
15 min read
Numbers having Unique (or Distinct) digits Given a range, print all numbers having unique digits. Examples : Input : 10 20Output : 10 12 13 14 15 16 17 18 19 20 (Except 11)Input : 1 10Output : 1 2 3 4 5 6 7 8 9 10Approach:As the problem is pretty simple, the only thing to be done is :-1- Find the digits one by one and keep marking visited di
10 min read