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
17 changes: 17 additions & 0 deletions 0001_TwoSum_HashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();

for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];

if (map.containsKey(complement)) {
return new int[] { map.get(complement), i };
}

map.put(nums[i], i);
}

return new int[] {};
}
}
34 changes: 34 additions & 0 deletions 0128_Longest_Consecutive_Sequence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.HashSet;

class Solution {
public int longestConsecutive(int[] nums) {

HashSet<Integer> set = new HashSet<>();

// Add all numbers to set
for (int num : nums) {
set.add(num);
}

int longest = 0;

for (int num : set) {

// Check if it's the start of a sequence
if (!set.contains(num - 1)) {

int currentNum = num;
int currentStreak = 1;

while (set.contains(currentNum + 1)) {
currentNum++;
currentStreak++;
}

longest = Math.max(longest, currentStreak);
}
}

return longest;
}
}
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# Hashing

## 1. Two Sum
Problem Link: https://leetcode.com/problems/two-sum/
Difficulty: Easy
### Approach : HashMap (Optimized)
Time Complexity: O(n)
Space Complexity: O(n)
### Concepts Used:
Array Traversal,HashMap,Complement Technique,Single Pass Algorithm,Key-Value Mapping

## 128. Longest Consecutive Sequence
Problem Link: https://leetcode.com/problems/longest-consecutive-sequence/
Difficulty: Medium
### Approach: HashSet (Optimized)
Time Complexity: O(n)
Space Complexity: O(n)
### Concepts Used:
HashSet, Sequence Pattern, Array Traversal
=======
# Prefix-Sum

## 523. Continuous Subarray Sum
Expand Down