Skip to content

Commit

Permalink
updated logic for optimised two sum
Browse files Browse the repository at this point in the history
  • Loading branch information
abhi472 committed Jan 5, 2024
1 parent 43899ca commit f5ebcae
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/kotlin/math/TwoSum.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ fun twoSum(nums: IntArray, target: Int): IntArray{
* of such pairs.
* @param nums Array of integers.
* @param target Integer target.
* @return Indices of the two numbers such that they add up to target.
* @return the two numbers such that they add up to target.
*/
fun twoSumOptimised(nums: IntArray, target: Int): IntArray{

val map: MutableMap<Int, Int> = HashMap()
for(num in nums) {
for(num in nums) {
val targetDiff = target - num;
if(map[targetDiff] == null)
map[num] = 1
else return intArrayOf(num, targetDiff)
}

return intArrayOf(0,1)
return intArrayOf()


}

0 comments on commit f5ebcae

Please sign in to comment.