Whats wrong with my logic for the below question :

Viewed 35

Problem: You are given two positive integers A and B. You need to construct two different binary strings (i.e, they are strings which consist of only 00s and 11s), which satisfy these two conditions:

Both the strings should be palindromes. Each string should have exactly AA 00s, and exactly BB 11s in them. Output Yes if two such different binary strings can be constructed and No otherwise.

My logic: if there are even number of zeros (A) then its always a pallindrome. but if there are odd number of zeros then there can be two cases that the total number of digits (i.e A+B) is either even or odd. In this case the odd number of digits will always be pallindrome.

Some rough work:

  • for even number of zeros (A) A=2, B=2 -> 0110 (true);; A=2, B=3 -> 01110 (true) ;; A=4, B=3 -> 0101010 (true)
  • for odd number of zeros A=3,B=2 -> 01010 (true) ;; A=3, B=3 -> 010010 (false) i started with placing 0 in the first and last place then 1 on the 2nd and 2nd last place then again 0 on 3rd and 3rd last place then 1 ... and so on. whenever i run out of numbers to place it cant be a pallindrome as it must be the same from front and back.

Hope i conveyed my question properly :P

0 Answers
Related