Given n and k, Construct a palindrome of size n using a binary number of size k repeating itself to wrap into the palindrome. The palindrome must always begin with 1 and contains maximum number of zeros.
Examples :
Input : n = 5, k = 3
Output : 11011
Explanation : the 3 sized substring is
110 combined twice and trimming the extra
0 in the end to give 11011.
Input : n = 2, k = 8
Output : 11
Explanation : the 8 sized substring is 11......
wrapped to two places to give 11.
The naive approach would be to try every palindrome of size k starting with 1 such that a palindrome of size n is formed. This approach has an exponential complexity.
A better way to do this is to initialize the k sized binary number with the index and connect the palindrome in the way it should be. Like last character of palindrome should match to first, find which indexes will be present at those locations and link them. Set every character linked with 0th index to 1 and the string is ready. This approach will have a linear complexity.
In this approach, first lay the index of the k sized binary to hold into an array, for example if n = 7, k = 3 arr becomes [0, 1, 2, 0, 1, 2, 0]. Following that in the connect chars graph, connect the indices of the k sized binary which should be same by going through the property of palindrome which is kth and (n – k – 1)th variable should be same, such that 0 is linked to 1(and vice versa), 1 is linked to 2(and vice versa) and so on. After that, check what is linked with 0 in connect chars array and make all of the associated indices one (because the first number should be non-zero) by using dfs approach. In the dfs, pass 0, the final answer string and the graph.
Begin by making the parent 1 and checking if its children are zero, if they are make them and their children 1. This makes only the required indices of the k sized string one, others are left zero. Finally, the answer contains the 0 to k – 1 indexes and corresponding to arr the digits are printed.
Implementation:
C++
#include <iostream>
#include <vector>
using namespace std;
void dfs( int parent, int ans[], vector< int > connectchars[])
{
ans[parent] = 1;
for ( int i = 0; i < connectchars[parent].size(); i++) {
if (!ans[connectchars[parent][i]])
dfs(connectchars[parent][i], ans, connectchars);
}
}
void printBinaryPalindrome( int n, int k)
{
int arr[n], ans[n] = { 0 };
vector< int > connectchars[k];
for ( int i = 0; i < n; i++)
arr[i] = i % k;
for ( int i = 0; i < n / 2; i++) {
connectchars[arr[i]].push_back(arr[n - i - 1]);
connectchars[arr[n - i - 1]].push_back(arr[i]);
}
dfs(0, ans, connectchars);
for ( int i = 0; i < n; i++)
cout << ans[arr[i]];
}
int main()
{
int n = 10, k = 4;
printBinaryPalindrome(n, k);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void dfs( int parent, int ans[],
Vector<Integer> connectchars[])
{
ans[parent] = 1 ;
for ( int i = 0 ; i < connectchars[parent].size(); i++)
{
if (ans[connectchars[parent].get(i)] != 1 )
dfs(connectchars[parent].get(i), ans, connectchars);
}
}
static void printBinaryPalindrome( int n, int k)
{
int []arr = new int [n];
int []ans = new int [n];
Vector<Integer> []connectchars = new Vector[k];
for ( int i = 0 ; i < k; i++)
connectchars[i] = new Vector<Integer>();
for ( int i = 0 ; i < n; i++)
arr[i] = i % k;
for ( int i = 0 ; i < n / 2 ; i++)
{
connectchars[arr[i]].add(arr[n - i - 1 ]);
connectchars[arr[n - i - 1 ]].add(arr[i]);
}
dfs( 0 , ans, connectchars);
for ( int i = 0 ; i < n; i++)
System.out.print(ans[arr[i]]);
}
public static void main(String[] args)
{
int n = 10 , k = 4 ;
printBinaryPalindrome(n, k);
}
}
|
Python3
def dfs(parent, ans, connectchars):
ans[parent] = 1
for i in range ( len (connectchars[parent])):
if ( not ans[connectchars[parent][i]]):
dfs(connectchars[parent][i], ans,
connectchars)
def printBinaryPalindrome(n, k):
arr = [ 0 ] * n
ans = [ 0 ] * n
connectchars = [[] for i in range (k)]
for i in range (n):
arr[i] = i % k
for i in range ( int (n / 2 )):
connectchars[arr[i]].append(arr[n - i - 1 ])
connectchars[arr[n - i - 1 ]].append(arr[i])
dfs( 0 , ans, connectchars)
for i in range (n):
print (ans[arr[i]], end = "")
if __name__ = = '__main__' :
n = 10
k = 4
printBinaryPalindrome(n, k)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void dfs( int parent, int []ans,
List< int > []connectchars)
{
ans[parent] = 1;
for ( int i = 0; i < connectchars[parent].Count; i++)
{
if (ans[connectchars[parent][i]] != 1)
dfs(connectchars[parent][i], ans, connectchars);
}
}
static void printBinaryPalindrome( int n, int k)
{
int []arr = new int [n];
int []ans = new int [n];
List< int > []connectchars = new List< int >[k];
for ( int i = 0; i < k; i++)
connectchars[i] = new List< int >();
for ( int i = 0; i < n; i++)
arr[i] = i % k;
for ( int i = 0; i < n / 2; i++)
{
connectchars[arr[i]].Add(arr[n - i - 1]);
connectchars[arr[n - i - 1]].Add(arr[i]);
}
dfs(0, ans, connectchars);
for ( int i = 0; i < n; i++)
Console.Write(ans[arr[i]]);
}
public static void Main(String[] args)
{
int n = 10, k = 4;
printBinaryPalindrome(n, k);
}
}
|
Javascript
<script>
function dfs(parent, ans, connectchars) {
ans[parent] = 1;
for (let i = 0; i < connectchars[parent].length; i++) {
if (!ans[connectchars[parent][i]])
dfs(connectchars[parent][i], ans, connectchars);
}
}
function printBinaryPalindrome(n, k) {
let arr = new Array(n), ans = new Array(n).fill(0);
let connectchars = new Array(k).fill(0).map(() => new Array(k).fill(0));
for (let i = 0; i < n; i++)
arr[i] = i % k;
for (let i = 0; i < n / 2; i++) {
connectchars[arr[i]].push(arr[n - i - 1]);
connectchars[arr[n - i - 1]].push(arr[i]);
}
dfs(0, ans, connectchars);
for (let i = 0; i < n; i++)
document.write(ans[arr[i]]);
}
let n = 10, k = 4;
printBinaryPalindrome(n, k);
</script>
|
Time Complexity: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!