From dce9c5d1f8b426c7024465a2a3f6b646d0b0554b Mon Sep 17 00:00:00 2001 From: Snehal Yeole Date: Mon, 1 Dec 2025 13:17:24 -0800 Subject: [PATCH 1/3] K Difference Pairs in an Array --- K_Diff_Pairs_Array.java | 40 ++++++++++++++++++++++++++++++++++++++++ Sample.java | 7 ------- 2 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 K_Diff_Pairs_Array.java delete mode 100644 Sample.java diff --git a/K_Diff_Pairs_Array.java b/K_Diff_Pairs_Array.java new file mode 100644 index 00000000..b7a1fc24 --- /dev/null +++ b/K_Diff_Pairs_Array.java @@ -0,0 +1,40 @@ +// Time Complexity : O(N) + O(N) = O(2N) = O(N) -> O(N) iterations to fill the map and O(N) to iterate through the map keys +// Space Complexity : O(N) -> To store 'N' elements in a map +// 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 +class Solution { + public int findPairs(int[] nums, int k) { + // Base condition + if(nums == null ||nums.length == 0 || k < 0){ + return 0; + } + + // 1. Initialize the counter to keep track of unique pairs + int pairs = 0; + + // 2. Create a HashMap to store frequenxy of each element in an array + HashMap counter = new HashMap<>(); + + // 3. 1st pass: Traverse array and fill the Map + for(int n : nums){ + counter.put(n, counter.getOrDefault(n, 0) + 1); // key: value, value = prev value count(0 or n) + 1 + } + + // 4. 2nd pass: Iterate over key-value pairs in the map using entrySet() + for(Map.Entry entry: counter.entrySet()){ + int numKey = entry.getKey(); // Get the key + int val = entry.getValue(); // Get the value + // If pair has distinct numbers and compliment of numKey is presen tin HashMap, increment pairs count. (numKey + k) to find compliment helps avoiding duplicate pairs ((1,4) and (4,1)) + if(k > 0 && counter.containsKey(numKey + k)){ + pairs++; + // If pair has same number i.e k=0 then their is more than one occurence of that number(val > 1) + }else if(k == 0 && val > 1){ + pairs++; + } + } + return pairs; + } +} \ No newline at end of file diff --git a/Sample.java b/Sample.java deleted file mode 100644 index f5c45b5f..00000000 --- a/Sample.java +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach \ No newline at end of file From 7376a79c8ebc6b70ff4c78961ad24b7df5ee4ca4 Mon Sep 17 00:00:00 2001 From: Snehal Yeole Date: Mon, 1 Dec 2025 13:37:25 -0800 Subject: [PATCH 2/3] Pascal's Triangle --- Pascals_Triangle | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Pascals_Triangle diff --git a/Pascals_Triangle b/Pascals_Triangle new file mode 100644 index 00000000..f065682f --- /dev/null +++ b/Pascals_Triangle @@ -0,0 +1,38 @@ +// Time Complexity : O(numRows^2) -> Two for loops each running for 'numRows=5' iterations +// Space Complexity : O(1) -> Output list created is returned as an output so no extra space used +// 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 +class Solution { + public List> generate(int numRows) { + // Create output List of Lists + List> result = new ArrayList<>(); + + // 'i' pointer - for each row + for(int i = 0; i < numRows; i++){ + // Create a list for each row + List row = new ArrayList<>(); + // 'j' pointer to fill all the columns in each row + for(int j = 0; j <= i; j++){ + // first and last columns in each row will be filled with 1 + if(j == 0 || j == i){ + row.add(1); + } + // All middle columns will be sum of two numbers directly above it in the previous row + else{ + int prev = result.get(i-1).get(j-1); + int next = result.get(i-1).get(j); + // Get both numbers and add sum to the row for that column + row.add(prev+next); + } + + } + // Add each row(list 'row') to the result list + result.add(row); + } + // Return list of lists + return result; + } +} \ No newline at end of file From ca11b2d7c464bef80a99cb9587b7970b82092081 Mon Sep 17 00:00:00 2001 From: Snehal Yeole Date: Mon, 1 Dec 2025 13:39:54 -0800 Subject: [PATCH 3/3] Pascal's Triangle --- Pascals_Triangle => Pascals_Triangle.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Pascals_Triangle => Pascals_Triangle.java (100%) diff --git a/Pascals_Triangle b/Pascals_Triangle.java similarity index 100% rename from Pascals_Triangle rename to Pascals_Triangle.java