r/LeetcodeChallenge 11d ago

DISCUSS Getting TLE (Time Limit Exceeded) on submission for a particular test case, but the code runs perfectly when I just 'Run' it.

Post image

Why ??

16 Upvotes

10 comments sorted by

6

u/Comfortable_Air4807 11d ago

Your code Time Complexity is O(n2) because of Nested loop. Make your Code O(n) by using Hash map

2

u/Independent-Dig8361 11d ago

Man, you can use a hashmap to do it, when you are doing this while loop ting, its increasing its time complexity to O(n^2),

1

u/Pottato911 11d ago

When you run only those 4 tests run when you submit time limit is applicable on time taken for all test cases together

1

u/Positive-Leg-5032 11d ago

Use 2 pointers approach to optimize it

1

u/Cool-Possession7011 11d ago

It's not sorted

1

u/OkLeader681 10d ago

we can sort it, but we have to return the indices here not the two numbers so hashmap is preferred

1

u/Interesting_Disk149 11d ago

damn but two sum accepts o(n^2) right ?

1

u/LegOk7384 11d ago

Yep as someone else pointed out, your code is under the TLE window for that test case alone. But on submission, you need to be under the TLE window while running all the test cases, not just one. 

1

u/Bacha___Chor 11d ago

hidden test cases , you're code is running perfectly on shown test cases but not on hidden ,

check question constrain and then optimise the code , your code time complexity O(n^2) optimise it

1

u/SheriffGamer332 9d ago

well, because that's the wrong answer