Find the smallest positive number which can not be represented by given digits
Last Updated :
14 Oct, 2022
Given an array arr[] of size 10 where arr[i] represents the frequency of the digit i. The task is to find the smallest positive number which can not be represented by the given digits.
Examples:
Input: arr[] = {2, 1, 1, 4, 0, 6, 3, 2, 2, 2}
Output: 4
Since the count of 4th digit is 0. So 4 can not be made.
Input : arr[] = {0, 1, 1, 1, 1, 1, 1, 1, 1, 1}
Output : 10
Since the count of 0th digit is 0. So smallest positive integer that can not be made is 10.
Input : arr[] = {2, 2, 1, 2, 1, 1, 3, 1, 1, 1}
Output : 22
To make 22 we require two 2’s but here the count of 2 is only 1.
Approach: The idea to solve the problem is to
Find the minimum index having the minimum value and form the number with that index.
Follow the steps mentioned below to implement the idea:
- Calculate the min value in the array starting from 1st index and also store its index.
- Now compare the min value with the value at 0th index.
- If the value at 0th index is less than the min value then either 10, 100, 1000 . . . can’t be expressed.
- If the value 0th index is greater than min value then:
- The resultant number will have the index of min value total (min value + 1) times.
- So print that index (min value + 1) times to get the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void expressDigit( int arr[], int n)
{
int min = 1000000000, index = 0, temp = 0;
temp = arr[0];
for ( int i = 1; i < 10; i++) {
if (arr[i] < min) {
min = arr[i];
index = i;
}
}
if (temp < min) {
cout << 1;
for ( int i = 1; i <= temp + 1; i++)
cout << 0;
}
else {
for ( int i = 0; i < min; i++)
cout << index;
cout << index;
}
}
int main()
{
int arr[] = { 2, 2, 1, 2, 1, 1, 3, 1, 1, 1 };
int N = 10;
expressDigit(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void expressDigit( int arr[], int n)
{
int min = 1000000000 , index = 0 , temp = 0 ;
temp = arr[ 0 ];
for ( int i = 1 ; i < 10 ; i++) {
if (arr[i] < min) {
min = arr[i];
index = i;
}
}
if (temp < min) {
System.out.print( 1 );
for ( int i = 1 ; i <= temp + 1 ; i++)
System.out.print( 0 );
}
else {
for ( int i = 0 ; i < min; i++)
System.out.print(index);
System.out.print(index);
}
}
public static void main(String[] args)
{
int arr[] = { 2 , 2 , 1 , 2 , 1 , 1 , 3 , 1 , 1 , 1 };
int N = 10 ;
expressDigit(arr, N);
}
}
|
Python3
def expressDigit(arr, n):
min = 1000000000
index = 0
temp = 0
temp = arr[ 0 ]
for i in range ( 1 , 10 ):
if (arr[i] < min ):
min = arr[i]
index = i
if (temp < min ):
print ( 1 , end = "")
for i in range ( 1 , temp + 1 ):
print ( 0 , end = "")
else :
for i in range ( min ):
print (index, end = "")
print (index)
arr = [ 2 , 2 , 1 , 2 , 1 ,
1 , 3 , 1 , 1 , 1 ]
N = 10
expressDigit(arr, N)
|
C#
using System;
class GFG {
static void expressDigit( int [] arr, int n)
{
int min = 1000000000, index = 0, temp = 0;
temp = arr[0];
for ( int i = 1; i < 10; i++) {
if (arr[i] < min) {
min = arr[i];
index = i;
}
}
if (temp < min) {
Console.Write(1);
for ( int i = 1; i <= temp + 1; i++)
Console.Write(0);
}
else {
for ( int i = 0; i < min; i++)
Console.Write(index);
Console.Write(index);
}
}
public static void Main(String[] args)
{
int [] arr = { 2, 2, 1, 2, 1, 1, 3, 1, 1, 1 };
int N = 10;
expressDigit(arr, N);
}
}
|
Javascript
<script>
function expressDigit(arr, n)
{
var min = 1000000000, index = 0, temp = 0;
temp = arr[0];
for (i = 1; i < 10; i++)
{
if (arr[i] < min)
{
min = arr[i];
index = i;
}
}
if (temp < min)
{
document.write(1);
for (i = 1; i <= temp + 1; i++)
document.write(0);
}
else {
for (i = 0; i < min; i++)
document.write(index);
document.write(index);
}
}
var arr = [ 2, 2, 1, 2, 1, 1, 3, 1, 1, 1 ];
var N = 10;
expressDigit(arr, N);
</script>
|
Time Complexity: O(min(A[i])), where min(A[i]) is the minimum element in the given array
Auxiliary Space: O(1), as constant extra space is required.
Share your thoughts in the comments
Please Login to comment...