https://www.codechef.com/viewsolution/29990336

@agrawall117 Sir, can you kindly explain your solution for the problem “Chef’s Birthday Gift” in Turing Cup Finals? I would request you to clear the following doubts

- How are you concluding the answer "YES " or “NO”

2.Kindly Explain the lines from 118 to 129.
- Why your solution works and how you came up with your solution?

Kindly help me to clear the above doubts so that I can also become a good coder like you.

Thanks in advance…

hello @sayan_1999

If at some point of time all string are equal so it is also possible to change into one sorted string ( and we will do this for our simplicity) then again all the string have to be equal and if not then at any point of time we can not make all strings into one one string . so u can check this by sorting all three string and if all r same then it may be YES but if not so answer surely “NO”. (Line No. 116).

now we will calculate how many min. swaps u have to take to sort the string (operation allowed.adjacent swap only). for this u can refer → https://www.geeksforgeeks.org/number-swaps-sort-adjacent-swapping-allowed/

now if all the parities are same for 3 strings then again we can do change into one string. (as with same parity i want to say 1st string requires 3 swaps, 2nd->5 , 3rd->9, so all values%2 are same hence we can change into one string as in 1 st string we can increase our swaps from 3 to 9 easily (repeat swaps) and same with string 2) so this cond. is in LINE No.117 .

now the last condition if parity is not same means 1st string requires 3 swap , 2-> 5 , 3rd->8 swaps .hence now see how u can change 1 and 2 nd string into 3rd string by increasing our swaps. so in this we can see if any adjacent characters are same in string a and b. so we can increased our swaps again and otherwise not.

hence in LINE no. 120 i m checking if 1string and 3 string have different parities so there will be some adjacent character same. (this is in my CHECK() function).

same with line 124.

Many many thanks Sir for your explanatory reply . I have been waiting for your reply since morning.Many many thanks for your help…@agrawall117

1 Like