{"ScriptPreparationCode":"\r\n// Naive appraoch - O(n^2)\r\n\r\nfunction twoSum1(arr, target) {\r\n for (let i = 0; i \u003C arr.length; i\u002B\u002B) {\r\n // for every integer of the array\r\n for (let j = i \u002B 1; j \u003C arr.length; j\u002B\u002B) {\r\n // go through every integer after it and check the sum\r\n if (arr[i] \u002B arr[j] === target) return true;\r\n }\r\n }\r\n\r\n return false;\r\n}\r\n\r\n\r\n// Using set - O(n)\r\n\r\nfunction twoSum2(arr, target) {\r\n // set to store difference of target and element\r\n const diffs = new Set();\r\n\r\n // iterate through array\r\n for (let element of arr) {\r\n // current element exists in diffs, we saw its complement before\r\n if (diffs.has(element)) return true;\r\n // otherwise add the complement to the diffs\r\n else diffs.add(target - element);\r\n }\r\n\r\n // target sum not found\r\n return false;\r\n}\r\n\r\n\r\n","TestCases":[{"Name":"twoSum1","Code":"const testArr = [];\r\nfor (let i = 0; i \u003C 10000; i\u002B\u002B) {\r\n testArr.push(i);\r\n}\r\ntwoSum1(testArr)","IsDeferred":false},{"Name":"twoSum2","Code":"const testArr = [];\r\nfor (let i = 0; i \u003C 10000; i\u002B\u002B) {\r\n testArr.push(i);\r\n}\r\ntwoSum2(testArr)","IsDeferred":false}]}