Hello @nagesh_reddy,
Nice to connect with you. I went through your code and here is what I think may be the reason for WA:
What is the need to append 0 (two times) to x?
In above code, first condition is proper. But second and third conditions are incorrect. This is because, after sorting, the last two entries need not be equal to “sm”. This can happen if the original sequence contains negative numbers. For example the prefix and suffix sums for sequence [3 -5 2] are [3 -2 0 2 -3 0], after sorting the second list, it becomes [-3 -2 0 0 2 3]. For this example, sm = 0 but the last two numbers (after sorting in ascending order) are NOT equal to 0.
The algo needs to be modified as follows:
After sorting the input numbers, delete two entries from it whose values are equal to sm. If the list does not contain 2 elements with value = sm, then, answer is 0.
Above modification will most probably solve WA problem with your code. I could not completely understand the logic and your code for the computation of prod, facto and finally the answer. The final formula is: n! / (n1! * n2! * n3! . . . nk!) * 2 ^ u But I could not find division happening anywhere in your code.
And the reason for TLE is because of the time consumed in computing prod, facto etc. You can pre-compute values for modulo and inverse modulo for factorials of numbers from 1 to 10^5. Here is link to the related discussion : Best known algos for calculating nCr % M - #2 by gamabunta
I am not a python expert. You can find my C++ code here: CodeChef: Practical coding for everyone
I hope it helps you in figuring out the problem and implementing correct solution. Best of luck to you and happy coding and learning ![]()
Thanks & Regards,
-Rushikesh.