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.
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 connectchars 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 connectchars 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.
Time Complexity : O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum steps to delete a string after repeated deletion of palindrome substrings
- Construct lexicographically smallest palindrome
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Longest sub string of 0's in a binary string which is repeated K times
- Construct a binary string following the given constraints
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Construct Binary Tree from Ancestor Matrix | Top Down Approach
- Find the last remaining element after repeated removal of odd and even indexed elements alternately
- Construct the Array using given bitwise AND, OR and XOR
- Binary String of given length that without a palindrome of size 3
- Python | Check if binary representation is palindrome
- Check if actual binary representation of a number is palindrome
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Check if Inorder traversal of a Binary Tree is palindrome or not
- Count of root to leaf paths whose permutation is palindrome in a Binary Tree
- Find maximum array sum after making all elements same with repeated subtraction
- Smallest element in an array that is repeated exactly 'k' times.
- Smallest element repeated exactly ‘k’ times (not limited to small range)
- Maximum subarray sum in an array created after repeated concatenation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.