Riyad_Hossain's blog

By Riyad_Hossain, history, 9 months ago, In English

Hello everyone,

Today, I attempted a problem having 6 seconds time limit. This problem has n and q values which need to be taken care of before selecting the expected solution which ultimately will fit into the constraints.

On this problem, as mentioned we need to consider the number of queries in each test case. Please read the full problem for better understanding: Problem Link

Key constraints: The sum of q over all test cases does not exceed 10^5, and the sum of n over all test cases does not exceed 10^5.

So, if I run a nested loop of n inside the loop of q then the time complexity would be O(q*n) that means (10^5)*(10^5) which is 10^10. The problem has a 6 seconds time limit. But my solution gave TLE. My Submission

Where do I have to optimize and how can I calculate such a complex scenario where the time limit is more than 1 second and have to consider the number of queries also (basically q value)?

Additional Question: if within 1 second, the 10^8 operation is executable then how many operations can I execute within 6 seconds?

Thanks in advance:)

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

»
9 months ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it

I will answer on the additional question. In 6 seconds you can do around 6 * 10^8 operations. 10^10 is 100 * 10^8, thus it takes around 100 seconds to run a nested loop of q and n.

And the situation isn't "complex", but it does require basic arithmetic, tough i know

  • »
    »
    9 months ago, # ^ |
      Vote: I like it +6 Vote: I do not like it
    about 'time complexity'
    • »
      »
      »
      9 months ago, # ^ |
        Vote: I like it +5 Vote: I do not like it

      Correct, but 10^8 operations per second is pretty accurate. You won't get into issues if you understand where it comes from and how does the complexity work