|
| 1 | +package com.rampatra.arrays; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.Set; |
| 5 | + |
| 6 | +/** |
| 7 | + * @author rampatra |
| 8 | + * @since 25/11/2018 |
| 9 | + */ |
| 10 | +public class LongestConsecutiveSubsequence { |
| 11 | + |
| 12 | + /** |
| 13 | + * Given an array of integers, find the length of the longest sub-sequence such that |
| 14 | + * elements in the subsequence are consecutive integers, the consecutive numbers can |
| 15 | + * be in any order. |
| 16 | + * <p> |
| 17 | + * Examples: |
| 18 | + * Input: arr[] = {1, 9, 3, 10, 4, 20, 2}; |
| 19 | + * Output: 4 |
| 20 | + * The subsequence {1, 3, 4, 2} is the longest subsequence |
| 21 | + * of consecutive elements |
| 22 | + * <p> |
| 23 | + * Input: arr[] = {36, 41, 56, 35, 44, 33, 34, 92, 43, 32, 42} |
| 24 | + * Output: 5 |
| 25 | + * The subsequence {36, 35, 33, 34, 32} is the longest subsequence |
| 26 | + * of consecutive elements. |
| 27 | + * |
| 28 | + * NOTE: You can also sort this array and check for consecutive elements. You can take this approach if interviewer |
| 29 | + * asks to solve with no additional space but do bear in mind that some sorting algorithms do require extra space. |
| 30 | + * |
| 31 | + * @param arr unsorted array of integers |
| 32 | + * @return the length of the longest consecutive subsequence |
| 33 | + */ |
| 34 | + private static int findLongestConsecutiveSubsequence(int[] arr) { |
| 35 | + int longestSubseqCount = 0; |
| 36 | + int subseqCount; |
| 37 | + int currElem; |
| 38 | + // add all numbers to a set to have O(1) time complexity for searching elements |
| 39 | + Set<Integer> numSet = new HashSet<>(); |
| 40 | + for (int n : arr) { |
| 41 | + numSet.add(n); |
| 42 | + } |
| 43 | + |
| 44 | + for (int n : arr) { |
| 45 | + subseqCount = 1; |
| 46 | + currElem = n; |
| 47 | + // check for the next consecutive elements |
| 48 | + while (numSet.contains(currElem + 1)) { |
| 49 | + numSet.remove(currElem); |
| 50 | + numSet.remove(currElem + 1); |
| 51 | + currElem++; |
| 52 | + subseqCount++; |
| 53 | + } |
| 54 | + // check for the previous consecutive elements |
| 55 | + while (numSet.contains(currElem - 1)) { |
| 56 | + numSet.remove(currElem); |
| 57 | + numSet.remove(currElem - 1); |
| 58 | + currElem--; |
| 59 | + subseqCount++; |
| 60 | + } |
| 61 | + // update longest counter if the length of the current subsequence is larger |
| 62 | + if (subseqCount > longestSubseqCount) { |
| 63 | + longestSubseqCount = subseqCount; |
| 64 | + } |
| 65 | + } |
| 66 | + return longestSubseqCount; |
| 67 | + } |
| 68 | + |
| 69 | + public static void main(String[] args) { |
| 70 | + System.out.println("{1, 9, 3, 10, 4, 20, 2}: " + findLongestConsecutiveSubsequence(new int[]{1, 9, 3, 10, 4, 20, 2})); |
| 71 | + System.out.println("{36, 41, 56, 35, 44, 33, 34, 92, 43, 32, 42}: " + |
| 72 | + findLongestConsecutiveSubsequence(new int[]{36, 41, 56, 35, 44, 33, 34, 92, 43, 32, 42})); |
| 73 | + System.out.println("{1}: " + findLongestConsecutiveSubsequence(new int[]{1})); |
| 74 | + System.out.println("{}: " + findLongestConsecutiveSubsequence(new int[]{})); |
| 75 | + System.out.println("{1,5,8,3}: " + findLongestConsecutiveSubsequence(new int[]{1, 5, 8, 3})); |
| 76 | + } |
| 77 | +} |
0 commit comments