Skip to content

Instantly share code, notes, and snippets.

@ritik-agrawal
Created August 10, 2023 08:48
Show Gist options
  • Save ritik-agrawal/df84d49bb37610ee31079022708fa64e to your computer and use it in GitHub Desktop.
Save ritik-agrawal/df84d49bb37610ee31079022708fa64e to your computer and use it in GitHub Desktop.
LeetCode: Max number of k-sums in a given integer array
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;
}
}
@ritik-agrawal
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment