Ad
  • Custom User Avatar

    Can anyone give hints on how to get sum by iterating throught the list only once? Passing all tests, but only can find sum num by iterating multiple times. So time-out on large arrays. At a wall and need to be pointed in a direction.

  • Default User Avatar

    Are you timing out on the fixed tests (when you press Test) or on the many large random tests (when you press Attempt) ?

    Your current approach is O(n^2) in any case, so it should time out for large random inputs:

    Maybe your "success" on your own computer could be to do with how you generate your random numbers?

    If you generate within a very small range eg the random values you put in your list are between "1" and "5" (for arguments sake) and your target is also small, say =3, then statistically you'd expect to encounter a valid pair almost certainly within the first ~25 elements or so - despite the fact that you think you are "testing" with list of 10 million elements.

    Try doing a "random" test on your PC where the last 2 elements in the 10-million elements are the desired pair - for example, try the following "random" list:

    test_list = [1] * 9_999_998 + [4,6] with target value s = 10

  • Custom User Avatar

    I'm looking at your code, and there's no way what you're saying is true.

    You have a nested loop, which makes the time complexity of your code O(n * n). That means roughly 10 mil * 10 mil iterations. No computer could possibly do this in the time frame you stated.

    What you need to do is get your code working by iterating the list only once.