✅Python3 || C++|| Java✅[Intervals,Greedy and DP] without any sort
Let\'s break down the code and its logic in more detail:\n\n1. std::unordered_map<int, std::pair<int, int>> mp;\n\n- mp is an unordered map used to store interv
Let\'s break down the code and its logic in more detail:\n\n1. `std::unordered_map<int, std::pair<int, int>> mp;`\n\n- `mp` is an unordered map used to store intervals (ranges) of consecutive numbers. Each key represents the right endpoint of an interval, and the corresponding value is a pair `(r, l)` where `r` is the right endpoint and `l` is the left endpoint of the interval.\n2. `std::unordered_map<int, bool> bl;`\n\n- `bl` is an unordered map used to keep track of whether an element `i` has been visited. If an element has been visited, its corresponding value in this map will be `true;` otherwise, it will be `false.`\n3. `int mx = 0;`\n\n- `mx` is a variable that keeps track of the maximum length of consecutive sequence found.\n4. The main loop iterates through each element `i` in the input `nums` array.\nInside the loop:\na. `if (bl[i]) { continue; }`\nIf the current element `i` has already been visited, skip it and continue to the next iteration of the loop.\nb. `bl[i] = true;`\nMark the current element `i` as visited by setting its corresponding value in the `bl` map to `true`.\nc. Initialize `l` and `r` to the current element `i`, representing the left and right endpoints of the current interval.\nd. Check if there is an interval with the right endpoint `i + 1` in the `mp` map using `mp.find(i + 1)`. If such an interval exists, update the right endpoint `r` to the right endpoint of that interval.\ne. Similarly, check if there is an interval with the right endpoint `i - 1` in the `mp` map using `mp.find(i - 1)`. If such an interval exists, update the left endpoint `l` to the left endpoint of that interval.\nf. Update the `mp` map:\nSet `mp[r]` to a pair `(r, l)`, indicating the interval with right endpoint `r` and left endpoint l.\nSet `mp[l]` to the same pair `(r, l)`.\ng. Calculate the length of the current consecutive sequence as `r - l + 1` and update the maximum length `mx` if this length is greater than the current maximum.\n\n5. Finally, return the maximum length mx, which represents the length of the longest consecutive sequence found in the input array.\nExplanation from chatgpt. The solution is from me \uD83D\uDE07.\n# Code\n<iframe src="https://leetcode.com/playground/PaQPcr82/shared" frameBorder="0" width="800" height="500"></iframe>\n\n![image.png](https://assets.leetcode.com/users/images/2e8406f3-dd08-44bf-8b74-b4c1e3d63cb1_1685761147.083231.png)
['C++', 'Java', 'Python3']