XYHUMOQ is almost same as this problem :
Let the number of subsequences of the form 1010β¦10 in a string be βaβ and the number of subsequences of the form 1010β¦101 be βbβ. So, you can represent any given binary string as a pair (a,b). By appending a 1 or 0 to such a string, you get either of these two strings (a+b,b), (a,a+b). This is because adding a β1β gives βaβ more sequences of type 1010β¦1 by extending each of the previously present sequences of type 1010β¦10, similarly for the other character, you get βbβ more sequences of type 101010β¦10.
Now, consider the reverse process. Given a string (a,b), you can go to either (a-b,b) or (a,b-a), by removing the last bit present in it. And, furthermore, there is only one way of going back this way, depending on whether a>b or b>a. This process is the same as the GCD algorithm. And, since the string must always begin with β1β, we must always reach (0,1). In the given problem, its mentioned that we need to have exactly βXβ subsequences of the form 101010β¦10. So, any candidate string will be represented by a pair of the form (X,Y).
Also, since we must reach (0,1), that means that the gcd of X and Y must be 1. This still leaves us with a lot of candidates for βYβ. But, since all strings must end with 0, Y will always be less than X. So, we just need to loop through 1,2,3,β¦X-1 as the candidates for βYβ, and for each of them, find the number of bits the string will have, and in case its equal to βnβ, we will see in how many places this string is different from the input string. Finally, we will print the one with the minimum number of differences as the answer. Overall, the complexity of this solution is O(XlogX).