Developer shares brute-force solution to LeetCode's Two Sum problem
A developer on DEV Community has shared a Python solution to LeetCode's classic Two Sum problem, one of the most commonly practiced coding challenges. The problem requires finding two distinct elements in an integer array whose values add up to a given target, then returning their indices. The proposed approach uses a nested loop to iterate over every possible pair of elements, checking that the two indices are different and that their values sum to the target. For example, given the array [2, 7, 11, 15] and a target of 9, the function correctly identifies indices 0 and 1, since 2 plus 7 equals 9. While straightforward and easy to understand, this brute-force method has O(n²) time complexity, making it less efficient for large inputs compared to hash-map-based alternatives.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in