From 0078c822c240e59be6c3a4b61b859b8de36b0653 Mon Sep 17 00:00:00 2001 From: dhruvil15 <76967484+dhruvil15@users.noreply.github.com> Date: Wed, 29 Apr 2026 20:45:04 -0400 Subject: [PATCH] Complete Competitive_Coding-3 --- Problem42.java | 33 +++++++++++++++++++++++++++++++++ Problem43.java | 27 +++++++++++++++++++++++++++ Sample.java | 7 ------- 3 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 Problem42.java create mode 100644 Problem43.java delete mode 100644 Sample.java diff --git a/Problem42.java b/Problem42.java new file mode 100644 index 00000000..a8fc08ec --- /dev/null +++ b/Problem42.java @@ -0,0 +1,33 @@ +// 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 + +class Solution { + public int findPairs(int[] nums, int k) { + Map map = new HashMap(); + int n = nums.length; + int count = 0; + + for (int i = 0; i < n; i++) { + map.put(nums[i], map.getOrDefault(nums[i],0)+1); + } + + for (int num : map.keySet()) { + if (k > 0) { + if (map.containsKey(num - k)) { + count++; + } + } else if (k == 0) { + if (map.get(num) > 1) { + count++; + } + } + } + + return count; + } +} \ No newline at end of file diff --git a/Problem43.java b/Problem43.java new file mode 100644 index 00000000..71154cf4 --- /dev/null +++ b/Problem43.java @@ -0,0 +1,27 @@ +// Time Complexity : O(n^2) +// Space Complexity : O(1) +// 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) { + + List> result = new ArrayList<>(); + result.add(Arrays.asList(1)); + + for(int i = 1; i < numRows; i++) { + List prevRow = result.get(i - 1); + List curRow = new ArrayList<>(); + curRow.add(1); + for(int j = 1; j < prevRow.size(); j++) { + curRow.add(prevRow.get(j - 1) + prevRow.get(j)); + } + curRow.add(1); + result.add(curRow); + } + + return result; + } +} 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