Created
August 10, 2023 08:48
-
-
Save ritik-agrawal/df84d49bb37610ee31079022708fa64e to your computer and use it in GitHub Desktop.
LeetCode: Max number of k-sums in a given integer array
This file contains 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 { | |
public int maxOperations(int[] nums, int k) { | |
var ret = 0; | |
Arrays.sort(nums); | |
var j = nums.length; | |
var i = 0; | |
while (i < j){ | |
if (nums[i]+nums[j] == k){ | |
ret++; | |
i++; | |
j--; | |
} else if (nums[i] + nums[j] < k){ | |
i++; | |
} else { | |
j--; | |
} | |
} | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Observation
In the process of solving the above problem, I used two approaches. One uses the map to store the value required to make the sum k to the list of indexes. This approach took 38 ms of runtime which beats 38% of the total solution submitted.
The second approach is presented above. This beats 99% of total solutions submitted with a runtime of 15 ms.