From 3ce37f1412bb3e476ff4ff0ba8dd31f84935cfb8 Mon Sep 17 00:00:00 2001 From: Harshitha Thondalapally Date: Mon, 12 Jan 2026 12:00:01 -0500 Subject: [PATCH] Completed competitive coding 3 --- src/KDiffPairs.java | 85 ++++++++++++++++++++++++++++++++++++++++ src/PascalsTriangle.java | 54 +++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 src/KDiffPairs.java create mode 100644 src/PascalsTriangle.java diff --git a/src/KDiffPairs.java b/src/KDiffPairs.java new file mode 100644 index 00000000..b058c560 --- /dev/null +++ b/src/KDiffPairs.java @@ -0,0 +1,85 @@ +/* +Problem: +Given an array of integers nums and an integer k, return the number of UNIQUE k-diff pairs in the array. +A k-diff pair is (a, b) such that |a - b| = k. + +Key points: +- Pairs are unique by VALUE, not by index. +- If k < 0, answer is 0 (because absolute difference can't be negative). + +Approach (HashMap frequency): +1) Build a frequency map: freq[x] = how many times x appears. +2) Iterate over each unique value "key" in the map: + - If k == 0: + We need pairs like (key, key). This is only valid if freq[key] >= 2. + - If k > 0: + We only count (key, key + k) to avoid double counting. + If (key + k) exists in the map, we count 1 unique pair. +3) Return count. +Why no double counting for k > 0? +- We only look in one direction: key -> key + k. + So (a, b) is counted once, not again as (b, a). +Time Complexity: + O(n) average time +Space Complexity: +- worst case O(n) +*/ + +import java.util.HashMap; +import java.util.Arrays; + +public class KDiffPairs { + + public int findPairs(int[] nums, int k) { + // Absolute difference cannot be negative + if (k < 0) return 0; + + HashMap freq = new HashMap<>(); + for (int x : nums) { + freq.put(x, freq.getOrDefault(x, 0) + 1); + } + + int count = 0; + + for (int key : freq.keySet()) { + if (k == 0) { + // Need at least two occurrences to form (key, key) + if (freq.get(key) > 1) count++; + } else { + // Count pair (key, key + k) only once + int complement = key + k; + if (freq.containsKey(complement)) count++; + } + } + + return count; + } + + public static void main(String[] args) { + KDiffPairs solver = new KDiffPairs(); + + int[][] testArrays = { + {3, 1, 4, 1, 5}, // k=2 => 2 + {1, 2, 3, 4, 5}, // k=1 => 4 + {1, 3, 1, 5, 4}, // k=0 => 1 + {1, 1, 1, 1}, // k=0 => 1 + {1, 1, 2, 2}, // k=1 => 1 + {-1, -2, -3}, // k=1 => 2 + {1, 2, 3} // k=-1 => 0 + }; + + int[] ks = {2, 1, 0, 0, 1, 1, -1}; + int[] expected = {2, 4, 1, 1, 1, 2, 0}; + + for (int i = 0; i < testArrays.length; i++) { + int ans = solver.findPairs(testArrays[i], ks[i]); + + System.out.println("Test " + (i + 1)); + System.out.println("nums: " + Arrays.toString(testArrays[i])); + System.out.println("k: " + ks[i]); + System.out.println("output: " + ans); + System.out.println("expected: " + expected[i]); + System.out.println(); + } + } +} diff --git a/src/PascalsTriangle.java b/src/PascalsTriangle.java new file mode 100644 index 00000000..590e7cb4 --- /dev/null +++ b/src/PascalsTriangle.java @@ -0,0 +1,54 @@ +/* +Problem: Pascal's Triangle +Given an integer numRows, return the first numRows of Pascal’s triangle. + +Rules: +- The first and last element of every row is 1. +- Any inner element at (row i, col j) is the sum of the two elements above it: + triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j] + +Approach: +1) Create an outer list to store all rows. +2) For each row i from 0 to numRows-1: + - Create a list of size (i + 1) + - For each column j from 0 to i: + - If j == 0 or j == i, add 1 (edges) + - Else add result[i-1][j-1] + result[i-1][j] (from previous row) +3) Return the triangle. + +Time Complexity: O(numRows^2) (total elements in triangle = 1 + 2 + ... + numRows) +Space Complexity: O(numRows^2) for storing the result (excluding output is not possible here since we must return it) +*/ + +import java.util.ArrayList; +import java.util.List; + +public class PascalsTriangle { + public List> generate(int numRows) { + List> result = new ArrayList>(); + if(numRows == 0){ + return result; + } + for(int i = 0; i < numRows; i++){ + List row = new ArrayList<>(i+1); + for(int j = 0; j <=i ; j++){ + if(j == 0 || j == i){ + row.add(1); + } + else{ + List temp = result.get(i-1); + row.add(temp.get(j-1) + temp.get(j)); + } + } + result.add(row); + } + return result; + } + public static void main(String[] args) { + PascalsTriangle t = new PascalsTriangle(); + List> result = t.generate(5); + for(List row : result){ + System.out.println(row); + } + } +}