From 36231bb1ac6b113f515e5b51d96aca34f389be25 Mon Sep 17 00:00:00 2001 From: sneha-tambade Date: Mon, 12 Jan 2026 18:23:59 -0800 Subject: [PATCH] Mock test problems solved --- KDiffPairsInAnArray.cs | 46 ++++++++++++++++++++++++++++++++++++++++++ PascalsTriangle.cs | 31 ++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 KDiffPairsInAnArray.cs create mode 100644 PascalsTriangle.cs diff --git a/KDiffPairsInAnArray.cs b/KDiffPairsInAnArray.cs new file mode 100644 index 00000000..6689fee4 --- /dev/null +++ b/KDiffPairsInAnArray.cs @@ -0,0 +1,46 @@ +//Time Complexity : O(n) +//Space Complexity : O(n) + +//Approach +// Maintain Dictionary with keys and elements from the array , values as frequency +// Iterate over keys and check sum = Key + K ,if dictionary contains that sum then count it as one pair +// If K is 0 then make sure frequency is greater than 1 then count. +public class Solution +{ + public int FindPairs(int[] nums, int k) + { + int count = 0; + Dictionary pair = new(); + for (int i = 0; i < nums.Length; i++) + { + if (!pair.ContainsKey(nums[i])) + { + pair.TryAdd(nums[i], 1); + } + else + { + pair[nums[i]]++; + } + } + foreach (var key in pair.Keys) + { + int sum = key + k; + if (k == 0) + { + int val = pair[key]; + if (val > 1) + { + count++; + } + + } + else if (pair.ContainsKey(sum)) + { + count++; + } + } + return count; + } +} + + diff --git a/PascalsTriangle.cs b/PascalsTriangle.cs new file mode 100644 index 00000000..6952209b --- /dev/null +++ b/PascalsTriangle.cs @@ -0,0 +1,31 @@ +//Time Complexity : O(n^2) +//Space Complexity : O(n^2) + +//Approach +// Start with the first row [1] and add it to the result. +// For each new row, set the first and last elements to 1. +// Fill middle elements using prevRow[j−1] + prevRow[j], then add the row to the result. + +public class Solution +{ + public IList> Generate(int numRows) + { + List> result = new List>(); + if (numRows == 0) + return result; + result.Add(new List() { 1 }); + for (int i = 1; i < numRows; i++) + { + List list = new List(); + List prevRow = (List)result[i - 1]; + list.Add(1); + for (int j = 1; j < prevRow.Count; j++) + { + list.Add(prevRow[j - 1] + prevRow[j]); + } + list.Add(1); + result.Add(list); + } + return result; + } +}