Getting TLE on Codeforces Round #304 (Div. 2), problem: (C) Soldier and Cards

codeforces
div2
tle

#1

Here is the Problem Link
Soldiers And Cards

My Submission link is My Submission

Please Provide me a Hint. I am getting TLE on Test Case 29. I am applying Brute Force Algorithm.
Help Required.

Thanks! :slight_smile:


#2

Note: In this case(i.e Test case 29) , both of the players stack reach to a situation from where Repetition of states starts i.e after few swaps the stacks become same(as some previous state) but not equal to initial situation as given in Input. so your equate() function always returns 0.and hence TLE.

  • Note you can keep a counter and assign it a upper bound and now if the number of iterations goes beyond upper bound value print -1.
    Hope it helps.

#3

Thanks Bro. I got it :slight_smile: