From e57eb4721a326e1e2e41a2b939ecbba85dcddcbd Mon Sep 17 00:00:00 2001 From: Manisha Rana Date: Mon, 12 Jan 2026 20:09:47 -0500 Subject: [PATCH] CC-3 Done --- KDiffPairs.java | 42 ++++++++++++++++++++++++++++++++++++++++++ PascalTriangle.java | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 KDiffPairs.java create mode 100644 PascalTriangle.java diff --git a/KDiffPairs.java b/KDiffPairs.java new file mode 100644 index 00000000..f400b020 --- /dev/null +++ b/KDiffPairs.java @@ -0,0 +1,42 @@ +// Time Complexity : O(N) +// Space Complexity : O(N), extra hashmap used +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +// Approach +// Since we need unique pair, we dont want any value to be considered multiple times, causing duplicate pairs. +// Need to handle edge case if k=0, then that occurs 2 or more times should be considered a pair. +// We will keep a frequency map, go over each key, try to find if there exists key-K value in the map, if yes, increase +// the result count. +import java.util.HashMap; +import java.util.Map; + +public class KDiffPairs { + + public int findPairs(int[] nums, int k) { + // Frequency map makes sure a value is not part of multiple pairs + Map temp = new HashMap<>(); + int count = 0; + for(int num: nums){ + if(temp.containsKey(num)){ + temp.put(num, temp.get(num)+1); + }else { + temp.put(num, 1); + } + // temp.merge(num, 1, Integer::sum); + } + + for(int key: temp.keySet()){ + if(k==0){ + // If no difference, then the same value can create a pair if it has occurred 2 or more times. + if(temp.get(key) > 1){ + count++; + } + }else if(temp.containsKey(key-k)){ + count++; + } + } + + return count; + } +} diff --git a/PascalTriangle.java b/PascalTriangle.java new file mode 100644 index 00000000..7ca31bf7 --- /dev/null +++ b/PascalTriangle.java @@ -0,0 +1,44 @@ +// Time Complexity : O(NXN), where N is number of rows +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +// Approach +// One approach is we can add 1 to beginning and ending of each row to avoid going beyond the boundaries. +// Second approach check if i/j is beyond boundary, dont fetch them just use 0 and add it to [i-1],[j] value. + +import java.util.ArrayList; +import java.util.List; + +public class PascalTriangle { + public List> generate(int numRows) { + List> result = new ArrayList<>(); + if(numRows == 0) return result; + // create first row + result.add(List.of(1)); + + for (int i = 1; i < numRows; i++) { + List temp = new ArrayList<>(); + for (int j = 0; j < numRows; j++) { + int top = 0; + // Check if [i-1][j] value exist by comparing j and size of previous row. Else use 0. + if (j < result.get(i-1).size()) { + top = result.get(i - 1).get(j); + } + int topLeft = 0; + // Check if [i-1][j-1] value exists, this will fail when j is at the boundary, j==0. Else use 0. + if (j > 0) { + topLeft = result.get(i - 1).get(j - 1); + } + temp.add(top + topLeft); + // As soon as required number of elements are processes break. + if(j == i){ + break; + } + } + result.add(temp); + } + + return result; + } +}