> For the complete documentation index, see [llms.txt](https://gopavasanth.gitbook.io/code/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gopavasanth.gitbook.io/code/sde-problems/day4-hashing.md).

# Day4: (Hashing)

[**2 Sum problem** ](https://leetcode.com/problems/two-sum/submissions/)\
Solved on: Aug 11 2020

```cpp
vector<int> twoSum(vector<int>& nums, int target) {
    map<int, int> m;
    int n=nums.size();
    int diff;
    for (int i=0;i<n;i++) {
        int ele = target-nums[i];
        if (m.count(ele)) {
            return {m[ele], i };
        }
        m[nums[i]] = i;
    }
    return {};
}
```

[**Longest Consecutive Sequence**](https://leetcode.com/problems/longest-consecutive-sequence/)\
Solved on: 20th Aug 2020

{% tabs %}
{% tab title="Python" %}

```cpp
def longestConsecutive(self, nums: List[int]) -> int:
    count=1
    nums=list(set(nums))
    n=len(nums)
    if (n==0):
        return 0
    nums.sort()
    print (nums)
    max_count=1
    for i in range(1, n):
        if nums[i-1]+1 == nums[i]:
            count=count+ 1
            if (count > max_count):
                max_count=count
        else:
            count=1
    return max_count
```

{% endtab %}

{% tab title="C++" %}

```cpp
int longestConsecutive(vector<int>& nums) {
    if (nums.size() == 0)
        return 0;
    else if (nums.size() == 1)
        return 1;
    
    set<int> S; 
    int n=nums.size();
    for (int i=0;i<n;i++){
        S.insert(nums[i]);
    }
    int count=1;
    int max=1; 
    
    auto e = prev(S.end(), 1); // last element
    
    for (auto itr=S.begin(); itr!=e; itr++) {
        cout << *itr << " ";
        if (*next(itr,1) == *itr + 1)  {
            count++;
            if (count>=max) {
                max=count;
            }
        } else {
            count=1;
        }
    }
    return max;
}

```

{% endtab %}
{% endtabs %}
