Find longest consecutive subsequence

June 24, 2022 · 1 min read

https://leetcode.com/problems/longest-consecutive-sequence/submissions/

class Solution {
public:
  int longestConsecutive(vector<int> &nums) {
    set<int> st(nums.begin(), nums.end());
    int maxLength = 0;

    for (auto it : st) {
      if (st.find(it - 1) == st.end()) {
        int currentNum = it;
        int currLength = 1;
        while (st.find(currentNum + 1) != st.end()) {
          currentNum += 1;
          currLength += 1;
        }

        maxLength = max(maxLength, currLength);
      }
    }
    return maxLength;
  }
};
Find maximum product subarray
Given an array of size N and a number K, find all elements that appear more than N/K times