code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

Longestconsecutivesequence

import java.util.HashSet;

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

class LongestConsecutiveSequence {

  static int func(int[] nums){
    int result = 0;
    int count = 0;
    HashSet<Integer> hs = new HashSet<>();
    for(int i = 0; i < nums.length;i++){
      hs.add(nums[i]);
    }
    for(int i = 0; i < nums.length; i++){
      if(!hs.contains(nums[i] - 1)){
        count = 1;
        while(hs.contains(nums[i] + count)){
          count++;
        }
        result = Math.max(result, count);
      }
    }
    return result;
  }

  public static void main(String[] args){
    int arr[] = new int[]{1,9,3,4,2,20};
    int result = func(arr);
    System.out.println(result);
  }
}

Created 2021-12-27T10:38:44+00:00 · Edit