TLE on one testcase for problem MAGICMOD

Hi there… I have been trying this problem since last night… Can someone point out why this code is giving TLE on one test set?

https://www.codechef.com/viewsolution/59389077
Problem Link: https://www.codechef.com/problems/MAGICMOD

Logic is same as given in editorial, I first subtract n*(n+1)/2 from sum of all elements and then check for all factors of the new sum if any of them satisfy the condition… for checking, I use a set to compare the permutation if a particular factor generates the permutation… so each factor takes O(N) time, which is well under constraints… still giving TLE.

Hey, Your approach is wrong is guess,
try this test case:
1
5
1 1 4 4 5

correct output: NO
your output: YES 6

I think test cases are weak.

1 Like

I used unordered_set in place of set and it worked. So I think this is because in unordered_set it takes O(1) to search and compare. I hope it helps. :slight_smile: Solution for your reference:
https://www.codechef.com/viewsolution/59392617

1 Like

Yeah it was pointed out in editorial’s comment section that testcases are weak. Thanks for pointing out the mistake :smiley:

Thanks :smiley:.