Construct a binary string following the given constraints
Last Updated :
12 Sep, 2022
Given three integers A, B and X. The task is to construct a binary string str which has exactly A number of 0’s and B number of 1’s provided there has to be at least X indices such that str[i] != str[i+1]. Inputs are such that there’s always a valid solution.
Examples:
Input: A = 2, B = 2, X = 1
Output: 1100
There are two 0’s and two 1’s and one (=X) index such that s[i] != s[i+1] (i.e. i = 1)
Input: A = 4, B = 3, X = 2
Output: 0111000
Approach:
- Divide x by 2 and store it in a variable d.
- Check if d is even and d / 2 != a, if the condition is true then print 0 and decrement d and a by 1.
- Loop from 1 to d and print 10 and in the end update a = a – d and b = b – d.
- Finally print the remaining 0’s and 1’s depending on the values of a and b.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int constructBinString( int a, int b, int x)
{
int d, i;
d = x / 2;
if (x % 2 == 0 && x / 2 != a) {
d--;
cout << 0;
a--;
}
for (i = 0; i < d; i++)
cout << "10" ;
a = a - d;
b = b - d;
for (i = 0; i < b; i++) {
cout << "1" ;
}
for (i = 0; i < a; i++) {
cout << "0" ;
}
}
int main()
{
int a = 4, b = 3, x = 2;
constructBinString(a, b, x);
return 0;
}
|
Java
class GFG
{
static void constructBinString( int a, int b, int x)
{
int d, i;
d = x / 2 ;
if (x % 2 == 0 && x / 2 != a)
{
d--;
System.out.print( "0" );
a--;
}
for (i = 0 ; i < d; i++)
System.out.print( "10" );
a = a - d;
b = b - d;
for (i = 0 ; i < b; i++)
{
System.out.print( "1" );
}
for (i = 0 ; i < a; i++)
{
System.out.print( "0" );
}
}
public static void main(String[] args)
{
int a = 4 , b = 3 , x = 2 ;
constructBinString(a, b, x);
}
}
|
Python3
def constructBinString(a, b, x):
d = x / / 2
if x % 2 = = 0 and x / / 2 ! = a:
d - = 1
print ( "0" , end = "")
a - = 1
for i in range (d):
print ( "10" , end = "")
a = a - d
b = b - d
for i in range (b):
print ( "1" , end = "")
for i in range (a):
print ( "0" , end = "")
if __name__ = = "__main__" :
a, b, x = 4 , 3 , 2
constructBinString(a, b, x)
|
C#
using System;
class GFG
{
static void constructBinString( int a, int b, int x)
{
int d, i;
d = x / 2;
if (x % 2 == 0 && x / 2 != a)
{
d--;
Console.Write( "0" );
a--;
}
for (i = 0; i < d; i++)
Console.Write( "10" );
a = a - d;
b = b - d;
for (i = 0; i < b; i++)
{
Console.Write( "1" );
}
for (i = 0; i < a; i++)
{
Console.Write( "0" );
}
}
public static void Main()
{
int a = 4, b = 3, x = 2;
constructBinString(a, b, x);
}
}
|
PHP
<?php
function constructBinString( $a , $b , $x )
{
$d ; $i ;
$d = $x / 2;
if ( $x % 2 == 0 && $x / 2 != $a )
{
$d --;
echo 0;
$a --;
}
for ( $i = 0; $i < $d ; $i ++)
echo "10" ;
$a = $a - $d ;
$b = $b - $d ;
for ( $i = 0; $i < $b ; $i ++)
{
echo "1" ;
}
for ( $i = 0; $i < $a ; $i ++)
{
echo "0" ;
}
}
$a = 4;
$b = 3;
$x = 2;
constructBinString( $a , $b , $x );
?>
|
Javascript
<script>
function constructBinString(a, b, x)
{
let d, i;
d = parseInt(x / 2, 10);
if (x % 2 == 0 && parseInt(x / 2, 10) != a)
{
d--;
document.write( "0" );
a--;
}
for (i = 0; i < d; i++)
document.write( "10" );
a = a - d;
b = b - d;
for (i = 0; i < b; i++)
{
document.write( "1" );
}
for (i = 0; i < a; i++)
{
document.write( "0" );
}
}
let a = 4, b = 3, x = 2;
constructBinString(a, b, x);
</script>
|
Complexity Analysis:
- Time Complexity: O(max(a,b,x))
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...