From 08bbb06e71cc0497001a82f7eb5f02f73785f96c Mon Sep 17 00:00:00 2001 From: abhayb94 Date: Mon, 12 Jan 2026 20:58:18 -0500 Subject: [PATCH] completed competitive coding 3 --- Problem1.java | 41 +++++++++++++++++++++++++++++++++++++++ Problem2.java | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 Problem1.java create mode 100644 Problem2.java diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..e26d679d --- /dev/null +++ b/Problem1.java @@ -0,0 +1,41 @@ +// Problem 1: Pascal's Triangle +//Leetcode : https://leetcode.com/problems/pascals-triangle/description/ + +/* + Approach: + The algorithm generates each row of Pascal's Triangle based on the previous row. + - Each row starts and ends with 1. + - Every other element at index 'j' in row 'i' is the sum of elements at 'j-1' and 'j' from row 'i-1'. + - We use a nested loop: the outer loop iterates through the number of rows, and the inner loop constructs each row. + + Time Complexity: O(numRows^2) + - We have two nested loops. The outer loop runs 'numRows' times. + - The inner loop runs 'i' times for each row, resulting in 1 + 2 + 3 + ... + numRows operations, which simplifies to O(numRows^2). + + Space Complexity: O(1) + - If we don't count the space required for the result list itself, we only use a few constant pointers and variables. + - If counting the output, it is O(numRows^2) to store the generated elements. +*/ + +class Solution { + public List> generate(int numRows) { + // store the final result + List> result = new ArrayList<>(); + + for(int i = 0; i < numRows; i++){ + List row = new ArrayList<>(); // this is the list to for each row + for (int j = 0; j <= i ; j++){ + if(j == 0 || j == i){ + row.add(1); + }else{ + Integer left = result.get(i-1).get(j-1); + Integer right = result.get(i-1).get(j); + row.add(left + right); + } + + } + result.add(row); + } + return result; + } +} \ No newline at end of file diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..d6d16ef0 --- /dev/null +++ b/Problem2.java @@ -0,0 +1,53 @@ +//Problem 2: K-diff Pairs in an Array +/* + Approach: + The goal is to find the number of unique pairs (nums[i], nums[j]) such that |nums[i] - nums[j]| = k. + - We use a HashMap to store each number from the input array as a key and its last seen index as the value. + - To handle unique pairs, we use a HashSet of Lists. Each pair is added to the set in a consistent order (e.g., smaller element first) to avoid duplicates. + - For each element `nums[i]`, we check if `nums[i] - k` or `nums[i] + k` exists in the map. + - If a target exists and its index is not the current index `i`, we found a pair. + - Finally, the size of the HashSet gives the count of unique k-diff pairs. + + Time Complexity: O(n) + - We iterate through the array once to populate the HashMap: O(n). + - We iterate through the array again to check for potential pairs: O(n). + - Map and Set operations (put, get, add) are O(1) on average. + + Space Complexity: O(n) + - The HashMap stores up to `n` elements: O(n). + - In the worst case, the HashSet could also store O(n) unique pairs. +*/ + +class Solution { + public int findPairs(int[] nums, int k) { + if(k< 0) return -1; + + // used hashmap because hashset was not suitable for this problem as it does not allow duplicate keys, which fails in scenario when k = 0 + HashMap map = new HashMap<>(); + int n = nums.length; + //put everything in map + for (int i = 0; i < n; i++){ + map.put(nums[i], i); + } + HashSet> result = new HashSet<>(); + + for(int i = 0; i < n ; i++ ){ + int element1 = nums[i] - k; + int element2 = nums[i] + k; + // adding smaller number to list first makes sure that no duplicate pairs are put in the set in reverse order and no need to sort the pairs + if(map.containsKey(element1) && map.get(element1)!=i){ + List tempList = new ArrayList<>(); + tempList.add(element1); + tempList.add(nums[i]); + result.add(tempList); + } + if(map.containsKey(element2) && map.get(element2)!=i){ + List tempList = new ArrayList<>(); + tempList.add(nums[i]); + tempList.add(element2); + result.add(tempList); + } + } + return result.size(); + } +} \ No newline at end of file