Form binary palindromic String with X 0s and Y 1s
Last Updated :
16 Aug, 2022
Given two integers M and N the task is to construct a binary palindromic string consisting of M occurrences of 0s and N occurrences of 1s.
Examples:
Input: M = 4, N = 3
Output: 0011100
Explanation: The string 0011100 is palindrome containing
4 occurrences of ‘0’ and 3 occurrences of ‘1’.
Input: M = 3, N = 3
Output: -1
Approach:
For the string to be a palindrome, atleast one among M and N should be even, otherwise if both M and N are odd then string can not be a palindrome.
Follow the below steps to solve the problem:
- If both M and N are odd then print -1.
- If N is Even, print ‘1’ N/2 times, then print ‘0’ M times, then again print ‘1’ N/2 times.
- If M is Even, print ‘0’ M/2 times, then print ‘1’ N times, then again print ‘0’ M/2 times.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
string buildString( int M, int N, char P, char Q)
{
string res = "" ;
for ( int i = 0; i < M / 2; i++)
res += P;
for ( int i = 0; i < N; i++)
res += Q;
for ( int i = 0; i < M / 2; i++)
res += P;
return res;
}
string makePalin( int M, int N)
{
if ((M & 1) && (N & 1)) {
return "-1" ;
}
else {
string ans = "" ;
if (N & 1) {
ans = buildString(M, N, '0' , '1' );
}
else {
ans = buildString(N, M, '1' , '0' );
}
return ans;
}
}
int main()
{
int M = 4;
int N = 3;
cout << makePalin(M, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static String buildString( int M, int N, char P, char Q)
{
String res = "" ;
for ( int i = 0 ; i < M / 2 ; i++)
res += P;
for ( int i = 0 ; i < N; i++)
res += Q;
for ( int i = 0 ; i < M / 2 ; i++)
res += P;
return res;
}
static String makePalin( int M, int N)
{
if ((M % 2 == 1 ) && (N % 2 == 1 )) {
return "-1" ;
}
else {
String ans = "" ;
if (N % 2 == 1 ) {
ans = buildString(M, N, '0' , '1' );
}
else {
ans = buildString(N, M, '1' , '0' );
}
return ans;
}
}
public static void main (String[] args)
{
int M = 4 ;
int N = 3 ;
System.out.println(makePalin(M, N));
}
}
|
Python3
def buildString(M, N, P, Q):
res = ""
for i in range (M / / 2 ):
res + = P
for i in range (N):
res + = Q
for i in range (M / / 2 ):
res + = P
return res
def makePalin(M, N):
if (((M & 1 ) ! = 0 ) and ((N & 1 ) ! = 0 )):
return "-1"
else :
ans = ""
if ((N & 1 ) ! = 0 ):
ans = buildString(M, N, '0' , '1' )
else :
ans = buildString(N, M, '1' , '0' )
return ans
if __name__ = = "__main__" :
M = 4
N = 3
print (makePalin(M, N))
|
C#
using System;
public class GFG{
public static string buildString( int M, int N, char P, char Q)
{
string res = "" ;
for ( int i = 0; i < M / 2; i++)
res += P;
for ( int i = 0; i < N; i++)
res += Q;
for ( int i = 0; i < M / 2; i++)
res += P;
return res;
}
public static string makePalin( int M, int N)
{
if ( Convert.ToBoolean(M & 1) && Convert.ToBoolean(N & 1)) {
return "-1" ;
}
else {
string ans = "" ;
if (Convert.ToBoolean(N & 1)) {
ans = buildString(M, N, '0' , '1' );
}
else {
ans = buildString(N, M, '1' , '0' );
}
return ans;
}
}
static public void Main (){
int M = 4;
int N = 3;
Console.WriteLine(makePalin(M, N));
}
}
|
Javascript
<script>
function buildString(M, N, P, Q)
{
let res = "" ;
for (let i = 0; i < M / 2; i++)
res += P;
for (let i = 0; i < N; i++)
res += Q;
for (let i = 0; i < M / 2; i++)
res += P;
return res;
}
function makePalin(M, N)
{
if ((M % 2==1) && (N %2==1)) {
return "-1" ;
}
else {
let ans = "" ;
if (N %2==1) {
ans = buildString(M, N, '0' , '1' );
}
else {
ans = buildString(N, M, '1' , '0' );
}
return ans;
}
}
let M = 4;
let N = 3;
document.write(makePalin(M, N));
</script>
|
Time Complexity: O(M+N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...