Created
September 7, 2024 16:47
-
-
Save tayadehritik/890f561f3f1a2d09ed37b29300d9f33f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| fun twoSum(nums: IntArray, target: Int): IntArray { | |
| val myMap = mutableMapOf<Int,Int>() | |
| for(i in 0..nums.size-1) { | |
| val difference = target - nums[i] | |
| val differenceIndex = myMap.getOrElse(difference){-1} | |
| if(differenceIndex != -1) { | |
| return intArrayOf(i,differenceIndex) | |
| } | |
| else{ | |
| myMap[nums[i]] = i | |
| } | |
| } | |
| return intArrayOf(0,0) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment