From d655d4f9027b18f4761b02b9a8e4d86371524441 Mon Sep 17 00:00:00 2001 From: Praniksha123 Date: Sat, 27 Jun 2026 17:27:17 +0530 Subject: [PATCH] Create coding3.java --- coding3.java | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 coding3.java diff --git a/coding3.java b/coding3.java new file mode 100644 index 00000000..b061e78c --- /dev/null +++ b/coding3.java @@ -0,0 +1,32 @@ +// Approach: +// 1. Store the frequency of each element using a HashMap. +// 2. If k == 0, count the elements whose frequency is at least 2. +// 3. Otherwise, iterate through the unique keys and check if (key + k) exists in the map. +// 4. Return the total count of valid k-diff pairs. + +// Time Complexity: O(n) +// Space Complexity: O(n) +class Solution { + public int findPairs(int[] nums, int k) { + int n=nums.length; + HashMap map=new HashMap<>(); + int cnt=0; + for(int i=0;i=2){ + cnt++; + } + } + }else{ + for(int key:map.keySet()){ + if(map.containsKey(key-k)){ + cnt++; + } + } + } + return cnt; + } +}