X

DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
Construct binary palindrome by repeated appending and trimming
Medium Accuracy: 67.43% Submissions: 389 Points: 4

Given n and k, Construct a palindrome of size n using the binary number of size k.To construct the palindrome you can use the binary number of size k as many times as you wish and also you can trim all the  zeros from the end.The palindrome must always begin with 1 and contains the maximum number of zeros.

Example 1 :

Input : n = 5, k = 3
Output : 11011
Explanation: Binary number of size 3 is
110.After combining 110
twice and trimming the extra 0 in
the end we get 11011


Example 2:

Input: n = 5, k = 8
Output: 10001
Explanation: Binary representation of 8 is
1000.After combining 1000 twice and trimming
the extra 0 in the end we get 10001.

You don't need to read or print anything. Your task is to complete the function binaryPalindrome() that takes two integers n, k and return a string of size n.

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

Constraints:
1 ≤ n ≤ 105
1 ≤ k ≤ 105