From 0927c5061339eb46e798b27c945a891da2a107b1 Mon Sep 17 00:00:00 2001 From: MeghaN28 Date: Mon, 4 May 2026 22:54:31 -0700 Subject: [PATCH] Competative Coding 3 --- PascalTraingle.java | 40 ++++++++++++++++++++++++++++++++++++++++ SumofK.java | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 PascalTraingle.java create mode 100644 SumofK.java diff --git a/PascalTraingle.java b/PascalTraingle.java new file mode 100644 index 00000000..b064861e --- /dev/null +++ b/PascalTraingle.java @@ -0,0 +1,40 @@ +// Time Complexity :O(numRows^2) +// Space Complexity :O(1) if we don't consider the output list, otherwise O(numRows^2) for the output list +// Did this code successfully run on Leetcode :Yes +// Any problem you faced while coding this :No , a bit of confusion in the inner loop but I got it after dry run + +// Your code here along with comments explaining your approach +//The first and last elements of every row are always 1 +//Every middle element is the sum of the two elements directly above it from the previous row +//So, if we already know the previous row, we can build the current row easily by applying this rule. +//If it's the first or last position, add 1 +//Otherwise, compute the value as: +//res[i-1][j-1] + res[i-1][j] (from the previous row) +import java.util.*; + +class Solution { + public List> generate(int numRows) { + + List> res = new ArrayList<>(); + + res.add(new ArrayList<>(Arrays.asList(1))); + + for (int i = 1; i < numRows; i++) { + + List list = new ArrayList<>(); + + for (int j = 0; j <= i; j++) { + + if (j == 0 || j == i) { + list.add(1); + } else { + list.add(res.get(i - 1).get(j - 1) + res.get(i - 1).get(j)); + } + } + + res.add(list); + } + + return res; + } +} \ No newline at end of file diff --git a/SumofK.java b/SumofK.java new file mode 100644 index 00000000..3302a660 --- /dev/null +++ b/SumofK.java @@ -0,0 +1,43 @@ +// Time Complexity : o(n) where n is the number of elements in the input array +// Space Complexity : O(n) where n is the number of unique elements in the input array +// 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 +// We can use a HashMap to store the frequency of each number in the input array as we need uniqy pairs and +// we can easily check if the pair exists in the map +// We can iterate through the map and check for each key if the pair (key + k) exists in the map +// If k is 0, we need to check if the frequency of the key is greater than 1 to count it as a valid pair, +// otherwise we can simply check if the pair exists +// We can keep a count of valid pairs and return it at the end +import java.util.*; + +class Solution { + public int findPairs(int[] nums, int k) { + + Map map = new HashMap<>(); + int count = 0; + + // build frequency map + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + + // check pairs + for (Map.Entry entry : map.entrySet()) { + + int key = entry.getKey(); + int value = entry.getValue(); + + if (k == 0 && value > 1) { + count++; + } + else if (k != 0 && map.containsKey(key + k)) { + count++; + } + } + + return count; + } +} \ No newline at end of file