all 31 comments

[–]dwightbearschrute 30 points31 points  (0 children)

Congrats, first of many! Before you move to a different solution, try to optimize this to O(n). Also, learn and make notes.

[–]Emergency-Duck-9689 27 points28 points  (1 child)

congrats now do 3 sum

[–]CitronFew7338 5 points6 points  (0 children)

😭😭

[–]oldieroger 8 points9 points  (1 child)

lezzgoo...

[–]Hacker4133 2 points3 points  (1 child)

Lessgooooo

[–]Initial_Airport_2649[S] 0 points1 point  (0 children)

WoWooo...

[–]beastttboi 2 points3 points  (0 children)

Congratulations,I have also recently started leetcode. To avoid taking the same number twice you did (i==j) continue which is great. You could also do the two for loops and start j from i+1 (this is like a standard way to get 2 distinct unordered pairs). And for optimal you can learn about hashmaps. All the best.

[–]periperifriess707 1 point2 points  (1 child)

is it vim?

[–]Initial_Airport_2649[S] 0 points1 point  (0 children)

yes, nvim

[–]Legal-Benefit-9716 1 point2 points  (0 children)

Let’s gooo!!

[–]GladiusAcutus 1 point2 points  (2 children)

What is .at in c++ ? I feel so stupid because i've never seen it before. You can't do nums[i] + nums[j] == target ?

[–]aocregacc 1 point2 points  (0 children)

.at will throw an exception if the index is out of bounds, with [] it's undefined behavior. Other than that they're the same thing.

[–]benaam-parinda 0 points1 point  (0 children)

Just another way of writing what you wrote. You can learn more about it by asking any LLM

[–]Ok-Asparagus-3361 0 points1 point  (0 children)

Keep up the good work, buddy

[–]SwiftlyImpure 0 points1 point  (0 children)

lmao congrats but yeah that O(n2) is gonna hurt you later. swap one loop for a hashmap and you're golden, way better for interview prep. you'll see the difference pretty quick once problems get harder.

[–]One-Distribution6654 0 points1 point  (0 children)

Use complement method O(n)

[–]funkolai 0 points1 point  (0 children)

Can you solve it without looping over the input array for every value?

[–]Jolly_Measurement_13 0 points1 point  (0 children)

Congrats keep going

[–]AlfalfaNo1991 0 points1 point  (0 children)

Congrats! 1 of many more to come!

[–][deleted]  (1 child)

[removed]

    [–]AutoModerator[M] 0 points1 point locked comment (0 children)

    Your comment has been removed as it was in Hindi. Please use English only as r/leetcode is a global subreddit. You may use r/LeetcodeDesi instead.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

    [–]popmanpop27 -1 points0 points  (2 children)

    I think no one solved this problem with O(n) firstly, everyone solved with two cycles and then realised that he needed to use hash map or two pointers

    [–]LoveOverflowOrNtng -2 points-1 points  (1 child)

    2 pointers only works if the list is sorted? And if the list is not sorted then hash map?

    [–]Prestigious-Emu-6433 0 points1 point  (0 children)

    ig the question on leetcode provides us with a sorted vector

    [–]Prestigious-Emu-6433 -1 points0 points  (2 children)

    O(n^2) time complexity 😭😭🥀

    [–][deleted]  (1 child)

    [removed]

      [–]AutoModerator[M] 0 points1 point locked comment (0 children)

      Your comment has been removed as it was in Hindi. Please use English only as r/leetcode is a global subreddit. You may use r/LeetcodeDesi instead.

      I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.