Construct smallest N-digit number possible by placing digits at positions specified by given array
Given two integers N and M and an 2D array arr[], the task is to construct the smallest possible integer of N digits ( without leading zeroes ) such that the arr[i][0]th digit from the left is arr[i][1]. If it is not possible to construct any such integer, print “No”. Otherwise, print that number.
Examples:
Input : N = 3, arr[]={{0, 7}, {2, 2}}
Output: 702
Explanation: According to the conditions, the 1st And 3rd digit should be equal to 7 and 2. The number should be of the form 7_2. Therefore, the minimum integer possible is 702.
Input : N = 3, arr[]={{1, 1}, {1, 3}}
Output: No
Naive Approach: Since the digits will always be from 0 to 9, the simplest approach is to check for a combination of every integer exceeding 0 and less than 10N by placing the digits according to the given conditions.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool check( int num, int N, int M,
vector<pair< int , char > >& A)
{
string temp = to_string(num);
if (temp.size() != N) {
return false ;
}
for ( int i = 0; i < M; i++) {
if (temp[A[i].first] != A[i].second) {
return false ;
}
}
return true ;
}
void find_num( int N, vector<pair< int , char > >& A)
{
int ans = -1;
for ( int i = 0; i < pow (10, N); i++) {
if (check(i, N, A.size(), A)) {
ans = i;
break ;
}
}
if (ans == -1)
cout << "No" ;
else
cout << ans;
}
int main()
{
int N = 3;
vector<pair< int , char > > A
= { { 0, '7' }, { 2, '2' }, { 0, '7' } };
find_num(N, A);
}
|
Java
import java.util.*;
class GFG{
static class pair
{
int first;
char second;
public pair( int first, char second)
{
this .first = first;
this .second = second;
}
};
static boolean check( int num, int N, int M,
Vector<pair> A)
{
String temp = String.valueOf(num);
if (temp.length() != N) {
return false ;
}
for ( int i = 0 ; i < M; i++) {
if (temp.charAt(A.get(i).first) != A.get(i).second) {
return false ;
}
}
return true ;
}
static void find_num( int N, Vector<pair> A)
{
int ans = - 1 ;
for ( int i = 0 ; i < Math.pow( 10 , N); i++) {
if (check(i, N, A.size(), A)) {
ans = i;
break ;
}
}
if (ans == - 1 )
System.out.print( "No" );
else
System.out.print(ans);
}
public static void main(String[] args)
{
int N = 3 ;
Vector<pair> A = new Vector<>();
A.add( new pair( 0 , '7' ));
A.add( new pair( 2 , '2' ));
A.add( new pair( 0 , '7' ));
find_num(N, A);
}
}
|
Python3
def check(num, N, M, A) :
temp = str (num)
if ( len (temp) ! = N) :
return False
for i in range (M) :
if (temp[A[i][ 0 ]] ! = A[i][ 1 ]) :
return False
return True
def find_num(N, A) :
ans = - 1
for i in range ( pow ( 10 , N)) :
if (check(i, N, len (A), A)) :
ans = i
break
if (ans = = - 1 ) :
print ( "No" )
else :
print (ans)
N = 3
A = [ [ 0 , '7' ], [ 2 , '2' ], [ 0 , '7' ] ]
find_num(N, A)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public class pair
{
public int first;
public char second;
public pair( int first, char second)
{
this .first = first;
this .second = second;
}
};
static bool check( int num, int N, int M,
List<pair> A)
{
String temp = String.Join( "" ,num);
if (temp.Length != N) {
return false ;
}
for ( int i = 0; i < M; i++) {
if (temp[A[i].first] != A[i].second) {
return false ;
}
}
return true ;
}
static void find_num( int N, List<pair> A)
{
int ans = -1;
for ( int i = 0; i < Math.Pow(10, N); i++) {
if (check(i, N, A.Count, A)) {
ans = i;
break ;
}
}
if (ans == -1)
Console.Write( "No" );
else
Console.Write(ans);
}
public static void Main(String[] args)
{
int N = 3;
List<pair> A = new List<pair>();
A.Add( new pair(0, '7' ));
A.Add( new pair(2, '2' ));
A.Add( new pair( 0, '7' ));
find_num(N, A);
}
}
|
Javascript
<script>
function check(num, N, M, A)
{
var temp = (num.toString());
if (temp.length != N) {
return false ;
}
for ( var i = 0; i < M; i++) {
if (temp[A[i][0]] != A[i][1]) {
return false ;
}
}
return true ;
}
function find_num(N, A)
{
var ans = -1;
for ( var i = 0; i < Math.pow(10, N); i++) {
if (check(i, N, A.length, A)) {
ans = i;
break ;
}
}
if (ans == -1)
document.write( "No" );
else
document.write( ans);
}
var N = 3;
var A = [ [ 0, '7' ], [ 2, '2' ], [ 0, '7' ] ];
find_num(N, A);
</script>
|
Time Complexity: O(10N * M)
Auxiliary Space: O(N)
Efficient Approach: Follow the steps below to solve the problem:
- Initialize a Map, say mp, to assign digits to the corresponding positions.
- If the first digit required to be placed is 0, then print “No” as the number cannot contain leading zeroes.
- Traverse the array arr[] and insert the digits at respective indices in the Map.
- Run a loop from 1 to N and for each i, check if a digit is assigned to the ith position in the Map. If present in the Map, add it to the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void find_num( int N, vector<pair< int , int > >& A)
{
map< int , int > mp;
for ( int i = 0; i < A.size(); i++) {
if (N > 1 and A[i].first == 0
and A[i].second == 0) {
cout << "No" ;
return ;
}
if (mp.find(A[i].first) != mp.end()
and mp[A[i].first] != A[i].second) {
cout << "No" ;
return ;
}
mp[A[i].first] = A[i].second;
}
string ans = "" ;
for ( int i = 0; i < N; i++) {
if (N > 1 and i == 0) {
if (mp.find(0) != mp.end()) {
ans += to_string(mp[0]);
}
else {
ans += to_string(1);
}
}
else {
ans += to_string(mp[i]);
}
}
cout << ans << "\n" ;
}
int main()
{
int N = 3;
vector<pair< int , int > > A
= { { 0, 7 }, { 2, 2 }, { 0, 7 } };
find_num(N, A);
}
|
Java
import java.util.*;
class GFG{
static class pair
{
int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static void find_num( int N, pair []A)
{
HashMap<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < A.length; i++)
{
if (N > 1 && A[i].first == 0 &&
A[i].second == 0 )
{
System.out.print( "No" );
return ;
}
if (mp.containsKey(A[i].first) &&
mp.get(A[i].first) != A[i].second)
{
System.out.print( "No" );
return ;
}
mp.put(A[i].first, A[i].second);
}
String ans = "" ;
for ( int i = 0 ; i < N; i++)
{
if (N > 1 && i == 0 )
{
if (mp.containsKey( 0 ))
{
ans += String.valueOf(mp.get( 0 ));
}
else
{
ans += String.valueOf( 1 );
}
}
else
{
if (mp.get(i) == null )
ans += String.valueOf( 0 );
else
ans += String.valueOf(mp.get(i));
}
}
System.out.print(ans + "\n" );
}
public static void main(String[] args)
{
int N = 3 ;
pair []A = { new pair( 0 , 7 ),
new pair( 2 , 2 ),
new pair( 0 , 7 )};
find_num(N, A);
}
}
|
Python3
def find_num(N, A) :
mp = {}
for i in range ( len (A)) :
if ((N > 1 ) and (A[i][ 0 ] = = 0 ) and (A[i][ 1 ] = = 0 )) :
print ( "No" )
return
if ((A[i][ 0 ] in mp) and (mp[A[i][ 0 ]] ! = A[i][ 1 ])) :
print ( "No" )
return
mp[A[i][ 0 ]] = A[i][ 1 ]
ans = ""
for i in range (N) :
if (N > 1 and i = = 0 ) :
if ( 0 in mp) :
ans = ans + str (mp[ 0 ])
else :
ans = ans + str ( 1 )
else :
if i in mp :
ans = ans + str (mp[i])
else :
ans = ans + str ( 0 )
print (ans)
N = 3
A = [ [ 0 , 7 ], [ 2 , 2 ], [ 0 , 7 ] ]
find_num(N, A)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public
class pair
{
public
int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static void find_num( int N, pair []A)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < A.Length; i++)
{
if (N > 1 && A[i].first == 0 &&
A[i].second == 0)
{
Console.Write( "No" );
return ;
}
if (mp.ContainsKey(A[i].first) &&
mp[A[i].first] != A[i].second)
{
Console.Write( "No" );
return ;
}
if (mp.ContainsKey(A[i].first))
mp[A[i].first] = A[i].second;
else
mp.Add(A[i].first, A[i].second);
}
String ans = "" ;
for ( int i = 0; i < N; i++)
{
if (N > 1 && i == 0)
{
if (mp.ContainsKey(0))
{
ans += String.Join( "" , mp[0]);
}
else
{
ans += String.Join( "" , 1);
}
}
else
{
if ( ! mp.ContainsKey(i) )
ans += String.Join( "" , 0);
else
ans += String.Join( "" , mp[i]);
}
}
Console.Write(ans + "\n" );
}
public static void Main(String[] args)
{
int N = 3;
pair []A = { new pair(0, 7),
new pair(2, 2),
new pair(0, 7)};
find_num(N, A);
}
}
|
Javascript
<script>
function find_num(N, A){
let mp = new Map()
for (let i=0;i<A.length;i++){
if ((N > 1) && (A[i][0] == 0) && (A[i][1] == 0)){
document.write( "No" )
return
}
if (mp.has(A[i][0])== true && (mp.get(A[i][0]) != A[i][1])){
document.write( "No" )
return
}
mp.set(A[i][0] , A[i][1])
}
let ans = ""
for (let i=0;i<N;i++){
if (N > 1 && i == 0){
if (mp.has(0))
ans = ans + mp.get(0).toString()
else {
let temp = 1
ans = ans + temp.toString(1)
}
}
else {
if (mp.has(i))
ans = ans + mp.get(i).toString()
else {
let temp = 0
ans = ans + temp.toString()
}
}
}
document.write(ans)
}
let N = 3
let A = [ [ 0, 7 ], [ 2, 2 ], [ 0, 7 ] ]
find_num(N, A)
</script>
|
Time Complexity: O(N + M)
Auxiliary Space: O(N)
Last Updated :
30 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...