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++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function that returns false if
// the number passed as argument contains
// digit(s) other than '0' or '1'
bool isBinary(int n)
{
while (n != 0) {
int temp = n % 10;
if (temp != 0 && temp != 1) {
return false;
}
n = n / 10;
}
return true;
}
//Function that checks whether the
//binary string can be formed or not
void formBinaryStr(int n, int a[])
{
bool flag = true;
// Empty string for storing
// the binary number
string s = "";
for (int i = 0; i < n; i++) {
// check if a[i] can be a
// part of the binary string
if (isBinary(a[i]))
// Conversion of int into string
s += to_string(a[i]);
else {
// if a[i] can't be a part
// then break the loop
flag = false;
break;
}
}
// possible to create binary string
if (flag)
cout << s << "\n";
// impossible to create binary string
else
cout << "-1\n";
}
// Driver code
int main()
{
int a[] = { 10, 1, 0, 11, 10 };
int N = sizeof(a) / sizeof(a[0]);
formBinaryStr(N, a);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class Solution
{
// Function that returns false if
// the number passed as argument contains
// digit(s) other than '0' or '1'
static boolean isBinary(int n)
{
while (n != 0) {
int temp = n % 10;
if (temp != 0 && temp != 1) {
return false;
}
n = n / 10;
}
return true;
}
//Function that checks whether the
//binary String can be formed or not
static void formBinaryStr(int n, int a[])
{
boolean flag = true;
// Empty String for storing
// the binary number
String s = "";
for (int i = 0; i < n; i++) {
// check if a[i] can be a
// part of the binary String
if (isBinary(a[i]))
// Conversion of int into String
s += ""+a[i];
else {
// if a[i] can't be a part
// then break the loop
flag = false;
break;
}
}
// possible to create binary String
if (flag)
System.out.print( s + "\n");
// impossible to create binary String
else
System.out.print( "-1\n");
}
// Driver code
public static void main(String args[])
{
int a[] = { 10, 1, 0, 11, 10 };
int N = a.length;
formBinaryStr(N, a);
}
}
//contributed by Arnab Kundu
Python3
# Python3 implementation of the approach
# Function that returns false if the
# number passed as argument contains
# digit(s) other than '0' or '1'
def isBinary(n):
while n != 0:
temp = n % 10
if temp != 0 and temp != 1:
return False
n = n // 10
return True
# Function that checks whether the
# binary string can be formed or not
def formBinaryStr(n, a):
flag = True
# Empty string for storing
# the binary number
s = ""
for i in range(0, n):
# check if a[i] can be a
# part of the binary string
if isBinary(a[i]) == True:
# Conversion of int into string
s += str(a[i])
else:
# if a[i] can't be a part
# then break the loop
flag = False
break
# possible to create binary string
if flag == True:
print(s)
# impossible to create binary string
else:
cout << "-1\n"
# Driver code
if __name__ == "__main__":
a = [10, 1, 0, 11, 10]
N = len(a)
formBinaryStr(N, a)
# This code is contributed by Rituraj Jain
C#
// C# implementation of the approach
using System;
public class Solution
{
// Function that returns false if
// the number passed as argument contains
// digit(s) other than '0' or '1'
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;
}
//Function that checks whether the
//binary String can be formed or not
public static void formBinaryStr(int n, int[] a)
{
bool flag = true;
// Empty String for storing
// the binary number
string s = "";
for (int i = 0; i < n; i++)
{
// check if a[i] can be a
// part of the binary String
if (isBinary(a[i]))
{
// Conversion of int into String
s += "" + a[i];
}
else
{
// if a[i] can't be a part
// then break the loop
flag = false;
break;
}
}
// possible to create binary String
if (flag)
{
Console.Write(s + "\n");
}
// impossible to create binary String
else
{
Console.Write("-1\n");
}
}
// Driver code
public static void Main(string[] args)
{
int[] a = new int[] {10, 1, 0, 11, 10};
int N = a.Length;
formBinaryStr(N, a);
}
}
// This code is contributed by Shrikant13
PHP
<?php
// PHP implementation of the approach
// Function that returns false if the
// number passed as argument contains
// digit(s) other than '0' or '1'
function isBinary($n)
{
while ($n != 0)
{
$temp = $n % 10;
if ($temp != 0 && $temp != 1)
{
return false;
}
$n = intval($n / 10);
}
return true;
}
// Function that checks whether the
// binary string can be formed or not
function formBinaryStr($n, &$a)
{
$flag = true;
// Empty string for storing
// the binary number
$s = "";
for ($i = 0; $i < $n; $i++)
{
// check if a[i] can be a
// part of the binary string
if (isBinary($a[$i]))
// Conversion of int into string
$s = $s.strval($a[$i]);
else
{
// if a[i] can't be a part
// then break the loop
$flag = false;
break;
}
}
// possible to create binary string
if ($flag)
echo $s . "\n";
// impossible to create binary string
else
echo "-1\n";
}
// Driver code
$a = array( 10, 1, 0, 11, 10 );
$N = sizeof($a) / sizeof($a[0]);
formBinaryStr($N, $a);
// This code is contributed by ita_c
?>
JavaScript
<script>
// Javascript implementation of the approach
// Function that returns false if
// the number passed as argument contains
// digit(s) other than '0' or '1'
function isBinary(n)
{
while (n != 0) {
var temp = n % 10;
if (temp != 0 && temp != 1) {
return false;
}
n = parseInt(n / 10);
}
return true;
}
// Function that checks whether the
// binary String can be formed or not
function formBinaryStr(n , a) {
var flag = true;
// Empty String for storing
// the binary number
var s = "";
for (i = 0; i < n; i++) {
// check if a[i] can be a
// part of the binary String
if (isBinary(a[i]))
// Conversion of var into String
s += "" + a[i];
else {
// if a[i] can't be a part
// then break the loop
flag = false;
break;
}
}
// possible to create binary String
if (flag)
document.write(s + "\n");
// impossible to create binary String
else
document.write("-1\n");
}
// Driver code
var a = [ 10, 1, 0, 11, 10 ];
var N = a.length;
formBinaryStr(N, a);
// This code contributed by Rajput-Ji
</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
Similar Reads
N-th character in the string made by concatenating natural numbers Given an integer N, the task is to find the N-th character in the string made by concatenating natural numbers (Integers beginning from 1). The starting string will be "12345678910111213..". Examples: Input: N = 3 Output: 3 3rd character in the string "12345678910111213.." is 3. Input: N = 11 Output
11 min read
Check if K '0's can be flipped such that Binary String contains no pair of adjacent '1's Given a binary string S of length N and an integer K, the task is to check if it is possible to flip K 0s such that the resulting string does not contain any pair of adjacent 1s. If it is possible to do so, then print "Yes". Otherwise, print "No". Input: S = "01001001000", K = 1Output: YesExplanatio
9 min read
Find the minimum number of distinct Substrings formed by 0s in Binary String Given two integers N and M, the task is to output the minimum number of different substrings that can be formed using the Binary String of length N having M number of set bits. Note: Two substrings let say S[x, y] and S[a, b] are different. If either x != a or y != b. Examples: Input: N = 3, M = 1Ou
6 min read
Mth bit in Nth binary string from a sequence generated by the given operations Given two integers N and M, generate a sequence of N binary strings by the following steps: S0 = "0"S1 = "1"Generate remaining strings by the equation Si = reverse(Si - 2) + reverse(Si - 1) The task is to find the Mth set bit in the Nth string. Examples: Input: N = 4, M = 3Output: 0Explanation:S0 ="
7 min read
Find the number obtained by concatenating binary representations of all numbers up to N Given an integer N, the task is to find the decimal value of the binary string formed by concatenating the binary representations of all numbers from 1 to N sequentially. Examples: Input: N = 12Output: 118505380540Explanation: The concatenation results in "1101110010111011110001001101010111100". The
5 min read
Check if a string is concatenation of another given string Given two strings str1 and str2 of length N and M respectively, the task is to check if the string str1 can be formed by concatenating the string str2 repetitively or not. Examples: Input: str1 = âabcabcabcâ, str2 = âabcâOutput: YesExplanation: Concatenating the string str2 thrice generates the stri
7 min read