Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions src/KDiffPairs.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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();
}
}
}
54 changes: 54 additions & 0 deletions src/PascalsTriangle.java
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if(numRows == 0){
return result;
}
for(int i = 0; i < numRows; i++){
List<Integer> row = new ArrayList<>(i+1);
for(int j = 0; j <=i ; j++){
if(j == 0 || j == i){
row.add(1);
}
else{
List<Integer> 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<List<Integer>> result = t.generate(5);
for(List<Integer> row : result){
System.out.println(row);
}
}
}