Victor_Luis123's blog

By Victor_Luis123, history, 8 months ago, In English

I solve the problem Sum of Three Values CSES problemset. According to my calculations, the n*n*log(n) solution (n<=5000) doesn't fit within time limit of 1s. So I used a fun fact for optimizing my solution. The fat is that if a<=b<=c and a+b+c=x, it yields that a<=ceil(x/3) and c>=floor(x/3) (that can be easily demonstrated using the method of proof by contradiction).

My solution is as follows: I sorted the array of numbers in non-descending order, then I used two nested for-loops(one from 1 to n and the other from n to 1) for iterating over a and c, respectively, using the aforementioned fact. I exit the loops if a or c stops holding the conditions. Now, for each possible a and c I used a map for verifying that there exists a number b such that a+b+c=x.

I'm not entirely sure what the worst-case complexity of my solution is, so I would be grateful if you could either attempt to hack it or provide a way to demonstrate the worst-case complexity.

Thanks in advance

  • Vote: I like it
  • -6
  • Vote: I do not like it

»
8 months ago, # |
  Vote: I like it +3 Vote: I do not like it

I made an n^2log(n) and got accepted LOL