diff --git a/KDiffPairsInArray.java b/KDiffPairsInArray.java new file mode 100644 index 00000000..44bacb6e --- /dev/null +++ b/KDiffPairsInArray.java @@ -0,0 +1,39 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach +// 1: We first create a frequency map for each element from the input array +// 2: For each key in the set, we check to see whether the complement exists +// 3: For k=0, we check to see if duplicates exist in the array +class Solution { + public int findPairs(int[] nums, int k) { + Map map = new HashMap<>(); + + for(int i = 0; i 0 && map.containsKey(complement)){ + result++; + } + else if(k==0 && map.get(key) >= 2 ){ + result++; + } + } + return result; + + } +} \ No newline at end of file