public static int longestConsecutive(int[] num) {
        // write you code here
        Map<Integer, Integer>seq=new HashMap<>();
        Set<Integer>s=new HashSet<>();
        int rel=0;
        for (int a:num) {
            if (s.contains(a))
                continue;
            s.add(a);
            Integer upper=seq.get(a+1);
            Integer lower=seq.get(a-1);
            if (upper!=null) {
                seq.remove(a+1);
            }
            else 
                upper=a;
            if (lower!=null)
                seq.remove(a-1);
            else 
                lower=a;
            seq.put(lower,upper);
            seq.put(upper,lower);
            rel=Math.max(rel, upper-lower+1);
        }
        return rel;
    }