diff --git a/PairsWithKDiff.swift b/PairsWithKDiff.swift new file mode 100644 index 00000000..80b01167 --- /dev/null +++ b/PairsWithKDiff.swift @@ -0,0 +1,53 @@ +// +// PairsWithKDiff.swift +// DSA-Practice +// +// Created by Paridhi Malviya on 4/5/26. +// + +class PairsWithKDiff { + + init() { + + } + + func findPairs(_ nums: [Int], _ k: Int) -> Int { + if (k < 0) { + return 0 + } + + var seen = Set() + //store the smaller element in result to avoid duplicates + var result = Set() + for num in nums { + if (seen.contains(num - k)) { + result.insert(num-k) + } + if (seen.contains(num + k)) { + result.insert(num) + } + seen.insert(num) + } + return result.count + } + + func findPairsUsingMap(_ nums: [Int], _ k: Int) -> Int { + var map = [Int: Int]() + + for num in nums { + map[num, default: 0] += 1 + } + var count = 0 + for (num, frequency) in map { + if (k == 0) { + if (map[num]! > 1) { + count += 1 + } + } else if (map[num + k] != nil) { + count += 1 + } + } + return count + } + +} diff --git a/PascalsTriangle.swift b/PascalsTriangle.swift new file mode 100644 index 00000000..50989470 --- /dev/null +++ b/PascalsTriangle.swift @@ -0,0 +1,38 @@ +// +// PascalsTriangle.swift +// DSA-Practice +// +// Created by Paridhi Malviya on 4/5/26. +// + +class PascalsTriangle { + + init() { + + } + + func generate(_ numRows: Int) -> [[Int]] { + var output = [[Int]]() + + for i in 0..