Check whether a binary string can be formed by concatenating given N numbers sequentially
Last Updated :
07 Sep, 2022
Given a sequence of ‘n’ numbers (without leading zeros), the task is to find whether it is possible to create a binary string by concatenating these numbers sequentially.
If possible, then print the binary string formed, otherwise print “-1”.
Examples :
Input: arr[] = {10, 11, 1, 0, 10}
Output: 10111010
All the numbers contain the digits ‘1’ and ‘0’ only. So it is possible to form a binary string by concatenating
these numbers sequentially which is 10111010.
Input: arr[] = {1, 2, 11, 10}
Output: -1
One of the numbers contains the digit ‘2’ which cannot be a part of any binary string.
So, the output is -1.
Approach: The main observation is that we can only concatenate those numbers which contain the digits ‘1’ and ‘0’ only. Otherwise, it is impossible to form a binary string.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
bool isBinary( int n)
{
while (n != 0) {
int temp = n % 10;
if (temp != 0 && temp != 1) {
return false ;
}
n = n / 10;
}
return true ;
}
void formBinaryStr( int n, int a[])
{
bool flag = true ;
string s = "" ;
for ( int i = 0; i < n; i++) {
if (isBinary(a[i]))
s += to_string(a[i]);
else {
flag = false ;
break ;
}
}
if (flag)
cout << s << "\n" ;
else
cout << "-1\n" ;
}
int main()
{
int a[] = { 10, 1, 0, 11, 10 };
int N = sizeof (a) / sizeof (a[0]);
formBinaryStr(N, a);
return 0;
}
|
Java
import java.util.*;
class Solution
{
static boolean isBinary( int n)
{
while (n != 0 ) {
int temp = n % 10 ;
if (temp != 0 && temp != 1 ) {
return false ;
}
n = n / 10 ;
}
return true ;
}
static void formBinaryStr( int n, int a[])
{
boolean flag = true ;
String s = "" ;
for ( int i = 0 ; i < n; i++) {
if (isBinary(a[i]))
s += "" +a[i];
else {
flag = false ;
break ;
}
}
if (flag)
System.out.print( s + "\n" );
else
System.out.print( "-1\n" );
}
public static void main(String args[])
{
int a[] = { 10 , 1 , 0 , 11 , 10 };
int N = a.length;
formBinaryStr(N, a);
}
}
|
Python3
def isBinary(n):
while n ! = 0 :
temp = n % 10
if temp ! = 0 and temp ! = 1 :
return False
n = n / / 10
return True
def formBinaryStr(n, a):
flag = True
s = ""
for i in range ( 0 , n):
if isBinary(a[i]) = = True :
s + = str (a[i])
else :
flag = False
break
if flag = = True :
print (s)
else :
cout << "-1\n"
if __name__ = = "__main__" :
a = [ 10 , 1 , 0 , 11 , 10 ]
N = len (a)
formBinaryStr(N, a)
|
C#
using System;
public class Solution
{
public static bool isBinary( int n)
{
while (n != 0)
{
int temp = n % 10;
if (temp != 0 && temp != 1)
{
return false ;
}
n = n / 10;
}
return true ;
}
public static void formBinaryStr( int n, int [] a)
{
bool flag = true ;
string s = "" ;
for ( int i = 0; i < n; i++)
{
if (isBinary(a[i]))
{
s += "" + a[i];
}
else
{
flag = false ;
break ;
}
}
if (flag)
{
Console.Write(s + "\n" );
}
else
{
Console.Write( "-1\n" );
}
}
public static void Main( string [] args)
{
int [] a = new int [] {10, 1, 0, 11, 10};
int N = a.Length;
formBinaryStr(N, a);
}
}
|
PHP
<?php
function isBinary( $n )
{
while ( $n != 0)
{
$temp = $n % 10;
if ( $temp != 0 && $temp != 1)
{
return false;
}
$n = intval ( $n / 10);
}
return true;
}
function formBinaryStr( $n , & $a )
{
$flag = true;
$s = "" ;
for ( $i = 0; $i < $n ; $i ++)
{
if (isBinary( $a [ $i ]))
$s = $s . strval ( $a [ $i ]);
else
{
$flag = false;
break ;
}
}
if ( $flag )
echo $s . "\n" ;
else
echo "-1\n" ;
}
$a = array ( 10, 1, 0, 11, 10 );
$N = sizeof( $a ) / sizeof( $a [0]);
formBinaryStr( $N , $a );
?>
|
Javascript
<script>
function isBinary(n)
{
while (n != 0) {
var temp = n % 10;
if (temp != 0 && temp != 1) {
return false ;
}
n = parseInt(n / 10);
}
return true ;
}
function formBinaryStr(n , a) {
var flag = true ;
var s = "" ;
for (i = 0; i < n; i++) {
if (isBinary(a[i]))
s += "" + a[i];
else {
flag = false ;
break ;
}
}
if (flag)
document.write(s + "\n" );
else
document.write( "-1\n" );
}
var a = [ 10, 1, 0, 11, 10 ];
var N = a.length;
formBinaryStr(N, a);
</script>
|
Complexity Analysis:
- Time Complexity: O(N*log(MAX)), where N is the length of the array and MAX is the maximum number in the array
- Auxiliary Complexity: O(M), where M is the length of the string
Share your thoughts in the comments
Please Login to comment...