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
42 changes: 42 additions & 0 deletions Problem-1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

// Time Complexity :O(n)
// Space Complexity :O(n)
// Did this code successfully run on Leetcode :yes
// Any problem you faced while coding this : a little near creating hashset logic


// Your code here along with comments explaining your approach
// Use a HashSet (prev_visited_elem) to track numbers seen so far so you can instantly check if a matching pair exists.
// For each number, compute x = num - k and y = num + k and check if either value was seen before — if yes, that forms a valid k-diff pair.
// Store each pair in a set (output) to avoid duplicates, and return the number of unique pairs found.

class Solution {
public int findPairs(int[] nums, int k) {
if (k < 0) return 0;

HashSet<Integer> prev_visited_elem = new HashSet<>();
HashSet<String> output = new HashSet<>();

for (int num : nums) {
int x = num - k;
int y = num + k;

if (prev_visited_elem.contains(x)) {
// store pair in normalized order (smaller first)
int a = x;
int b = num;
output.add(a + "," + b);
}

if (prev_visited_elem.contains(y)) {
int a = num;
int b = y;
output.add(a + "," + b);
}

prev_visited_elem.add(num);
}

return output.size();
}
}
26 changes: 26 additions & 0 deletions Problem-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Time Complexity :O(n)
# Space Complexity :O(n)
# Did this code successfully run on Leetcode :yes
# Any problem you faced while coding this : a little near creating hashset logic


# Your code here along with comments explaining your approach
# Use a HashSet (prev_visited_elem) to track numbers seen so far so you can instantly check if a matching pair exists.
# For each number, compute x = num - k and y = num + k and check if either value was seen before — if yes, that forms a valid k-diff pair.
# Store each pair in a set (output) to avoid duplicates, and return the number of unique pairs found.

class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
if k < 0:
return 0
prev_visited_elem = set()
output = set()
for num in nums:
x = num - k
y = num + k
if x in prev_visited_elem:
output.add((x, num))
if y in prev_visited_elem:
output.add((num, y))
prev_visited_elem.add(num)
return len(output)
52 changes: 52 additions & 0 deletions Problem-2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Time Complexity : O(n^2)
// Space Complexity :O(n^2)
// Did this code successfully run on Leetcode :yes
// Any problem you faced while coding this :no


// Your code here along with comments explaining your approach
// Start with the first two base rows of Pascal’s Triangle: [1] and [1, 1].
// For each new row, compute inner values by adding two adjacent numbers from the previous row.
// Append 1 at the start and end of each row to complete it, and repeat until all rows are built.

class Solution {
public List<List<Integer>> generate(int numRows) {
if (numRows == 1) {
List<List<Integer>> result = new ArrayList<>();
result.add(Arrays.asList(1));
return result;
}

if (numRows == 2) {
List<List<Integer>> result = new ArrayList<>();
result.add(Arrays.asList(1));
result.add(Arrays.asList(1, 1));
return result;
}

List<List<Integer>> output = new ArrayList<>();
output.add(Arrays.asList(1));
output.add(Arrays.asList(1, 1));

for (int i = 2; i < numRows; i++) {
List<Integer> prev = output.get(output.size() - 1);
List<Integer> newRow = buildRow(prev, i);
output.add(newRow);
}

return output;
}

// Helper method similar to your Python res_row()
private List<Integer> buildRow(List<Integer> prev, int i) {
List<Integer> row = new ArrayList<>();
row.add(1);

for (int k = 1; k < i; k++) {
row.add(prev.get(k - 1) + prev.get(k));
}

row.add(1);
return row;
}
}
32 changes: 32 additions & 0 deletions Problem-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Time Complexity : O(n^2)
# Space Complexity :O(n^2)
# Did this code successfully run on Leetcode :yes
# Any problem you faced while coding this :no


# Your code here along with comments explaining your approach
# Start with the first two base rows of Pascal’s Triangle: [1] and [1, 1].
# For each new row, compute inner values by adding two adjacent numbers from the previous row.
# Append 1 at the start and end of each row to complete it, and repeat until all rows are built.

class Solution:
def generate(self, numRows: int) -> List[List[int]]:
if numRows == 1:
return [[1]]
if numRows == 2:
return [[1],[1,1]]
output = [[1],[1,1]]
def res_row(prev,i):
result = []
result.append(1)
for k in range(1,i):
result.append(prev[k-1]+prev[k])
result.append(1)
return result
for i in range(2,numRows):
prev= output[-1]
numrow_result = res_row(prev,i)
output.append(numrow_result)
return output