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++
// C++ code for above approach
#include <iostream>
using namespace std;
// Function to build the palindromic
// string if possible
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;
}
// Function to check if it is possible
// to make the string palindrome
string makePalin(int M, int N)
{
// If both M and N are odd then it is
// impossible to make the
// string palindrome
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;
}
}
// Driver Code
int main()
{
int M = 4;
int N = 3;
// Function Call
cout << makePalin(M, N);
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
class GFG
{
// Java code for above approach
// Function to build the palindromic
// string if possible
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;
}
// Function to check if it is possible
// to make the string palindrome
static String makePalin(int M, int N)
{
// If both M and N are odd then it is
// impossible to make the
// string palindrome
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;
}
}
// Driver Code
public static void main (String[] args)
{
int M = 4;
int N = 3;
// Function Call
System.out.println(makePalin(M, N));
}
}
// This code is contributed by satwik4409.
Python3
# Python code for above approach
# Function to build the palindromic
# string if possible
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
# Function to check if it is possible
# to make the string palindrome
def makePalin(M, N):
# If both M and N are odd then it is
# impossible to make the
# string palindrome
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
# Driver Code
if __name__ == "__main__":
M = 4
N = 3
# Function Call
print(makePalin(M, N))
# This code is contributed by Rohit Pradhan
C#
using System;
public class GFG{
// Function to build the palindromic
// string if possible
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;
}
// Function to check if it is possible
// to make the string palindrome
public static string makePalin(int M, int N)
{
// If both M and N are odd then it is
// impossible to make the
// string palindrome
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;
// Function Call
Console.WriteLine(makePalin(M, N));
}
}
// This code is contributed by akashish__
JavaScript
<script>
// JavaScript code for the above approach
// Function to build the palindromic
// string if possible
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 to check if it is possible
// to make the string palindrome
function makePalin(M, N)
{
// If both M and N are odd then it is
// impossible to make the
// string palindrome
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;
}
}
// Driver Code
let M = 4;
let N = 3;
// Function Call
document.write(makePalin(M, N));
// This code is contributed by sanjoy_62.
</script>
Time Complexity: O(M+N)
Auxiliary Space: O(1)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem