Construct binary palindrome by repeated appending and trimming
Medium Accuracy: 40.0% Submissions: 5 Points: 4

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 the maximum number of zeros.

Example 1:

​Input : N = 5 and K = 3
Output : 11011
The 3 sized substring is 110 combined 
twice and trimming the extra 0 in the 
end to give 11011.

​Example 2:

Input : N = 2 and  K = 8 
Output :  11 


Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function binaryPalindrome() that takes two integers N, K and return the output is a palindrome of size N. The driver code takes care of the printing.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).


1 ≤ N ≤ 105
1 ≤ K ≤ 105

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Construct binary palindrome by repeated appending and trimming

Output Window