Check whether a binary string can be formed by concatenating given N numbers sequentially | Java implementation of the approach ; Function that returns false if the number passed as argument contains digit ( s ) other than '0' or '1' ; Function that checks whether the binary String can be formed or not ; Empty String for storing the binary number ; check if a [ i ] can be a part of the binary String ; Conversion of int into String ; if a [ i ] can 't be a part  then break the loop ; possible to create binary String ; impossible to create binary String ; Driver code
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 + "NEW_LINE"); else System . out . print ( "-1NEW_LINE"); } public static void main ( String args [ ] ) { int a [ ] = { 10 , 1 , 0 , 11 , 10 } ; int N = a . length ; formBinaryStr ( N , a ) ; } }