|
| 1 | +/* |
| 2 | +You are given an array of unique integers that contain numbers in random order. You have to find the longest possible sequence of consecutive numbers using the numbers from given array. |
| 3 | +You need to return the output array which contains starting and ending element. If the length of the longest possible sequence is one, then the output array must contain only single element. |
| 4 | +Note: |
| 5 | +1. Best solution takes O(n) time. |
| 6 | +2. If two sequences are of equal length, then return the sequence starting with the number whose occurrence is earlier in the array. |
| 7 | + |
| 8 | +Input format: |
| 9 | +The first line of input contains an integer, that denotes the value of the size of the array. Let us denote it with the symbol n. |
| 10 | +The following line contains n space separated integers, that denote the value of the elements of the array. |
| 11 | + |
| 12 | +Output format: |
| 13 | +The first and only line of output contains starting and ending element of the longest consecutive sequence. If the length of longest consecutive sequence, then just print the starting element. |
| 14 | + |
| 15 | +Constraints : |
| 16 | +0 <= n <= 10^6 |
| 17 | +Time Limit: 1 sec |
| 18 | + |
| 19 | +Sample Input 1 : |
| 20 | +13 |
| 21 | +2 12 9 16 10 5 3 20 25 11 1 8 6 |
| 22 | +Sample Output 1 : |
| 23 | +8 12 |
| 24 | + |
| 25 | +Sample Input 2 : |
| 26 | +7 |
| 27 | +3 7 2 1 9 8 41 |
| 28 | +Sample Output 2 : |
| 29 | +7 9 |
| 30 | +Explanation: Sequence should be of consecutive numbers. Here we have 2 sequences with same length i.e. [1, 2, 3] and [7, 8, 9], but we should select [7, 8, 9] because the starting point of [7, 8, 9] comes first in input array and therefore, the output will be 7 9, as we have to print starting and ending element of the longest consecutive sequence. |
| 31 | + |
| 32 | +Sample Input 3 : |
| 33 | +7 |
| 34 | +15 24 23 12 19 11 16 |
| 35 | +Sample Output 3 : |
| 36 | +15 16 |
| 37 | +*/ |
| 38 | +import java.util.Map; |
| 39 | +import java.util.HashMap; |
| 40 | +import java.util.ArrayList; |
| 41 | + |
| 42 | +public class Solution { |
| 43 | + public static ArrayList<Integer> longestConsecutiveIncreasingSequence(int[] arr) { |
| 44 | + /* Your class should be named Solution |
| 45 | + * Don't write main(). |
| 46 | + * Don't read input, it is passed as function argument. |
| 47 | + * Return output and don't print it. |
| 48 | + * Taking input and printing output is handled automatically. |
| 49 | + */ |
| 50 | + ArrayList<Integer> output = new ArrayList<>(); |
| 51 | + HashMap<Integer, Boolean> map = new HashMap<>(); |
| 52 | + HashMap<Integer, Integer> lenMap = new HashMap<>(); |
| 53 | + for (int i=0;i<arr.length;i++) |
| 54 | + { |
| 55 | + map.put(arr[i],true); |
| 56 | + } |
| 57 | + int maxStart=-1,maxLen=0; |
| 58 | + boolean startCheck=true; |
| 59 | + |
| 60 | + for (int i: arr) |
| 61 | + { |
| 62 | + if (map.get(i)) |
| 63 | + { |
| 64 | + int currStart=i,currLen=1; |
| 65 | + //System.out.println("Current start of sequence: "+currStart); |
| 66 | + //System.out.println("Current length of sequence: "+currLen); |
| 67 | + boolean flag=true; |
| 68 | + map.put(i,false); |
| 69 | + |
| 70 | + int ahead=i+1; |
| 71 | + //System.out.println("Checking for values ahead"); |
| 72 | + //System.out.println(); |
| 73 | + while(flag) |
| 74 | + { |
| 75 | + if(map.containsKey(ahead)) |
| 76 | + { |
| 77 | + //System.out.println(ahead+" is included in sequence and status updates to false"); |
| 78 | + currLen=currLen+1; |
| 79 | + map.put(ahead,false); |
| 80 | + ahead=ahead+1; |
| 81 | + //System.out.println("Current length of sequence: "+currLen); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + flag=false; |
| 86 | + } |
| 87 | + } |
| 88 | + flag=true; |
| 89 | + int before=i-1; |
| 90 | + //System.out.println(); |
| 91 | + //System.out.println("Checking for values before"); |
| 92 | + while(flag) |
| 93 | + { |
| 94 | + if(map.containsKey(before)) |
| 95 | + { |
| 96 | + //System.out.println(before+" is included in sequence and status updates to false"); |
| 97 | + currLen=currLen+1; |
| 98 | + currStart=before; |
| 99 | + map.put(before,false); |
| 100 | + before=before-1; |
| 101 | + //System.out.println("Current length of sequence: "+currLen); |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + flag=false; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + System.out.println(); |
| 110 | + if (currLen>=maxLen) |
| 111 | + { |
| 112 | + maxLen=currLen; |
| 113 | + maxStart=currStart; |
| 114 | + lenMap.put(maxStart,maxLen); |
| 115 | + } |
| 116 | + } |
| 117 | + /* |
| 118 | + else |
| 119 | + { |
| 120 | + System.out.println("Element already considered in previous sequence"); |
| 121 | + } |
| 122 | + System.out.println(); |
| 123 | + */ |
| 124 | + } |
| 125 | + |
| 126 | + //System.out.println("Current start point and length: "+ maxStart+", "+maxLen); |
| 127 | + /* |
| 128 | + for (Integer i: lenMap.keySet()) |
| 129 | + { |
| 130 | + System.out.println(i+": "+lenMap.get(i)); |
| 131 | + } |
| 132 | + System.out.println(); |
| 133 | + */ |
| 134 | + |
| 135 | + |
| 136 | + for (int i=0;i<arr.length;i++) |
| 137 | + { |
| 138 | + if (lenMap.containsKey(arr[i]) && lenMap.get(arr[i])>=maxLen) |
| 139 | + { |
| 140 | + maxStart=arr[i]; |
| 141 | + maxLen=lenMap.get(arr[i]); |
| 142 | + break; |
| 143 | + } |
| 144 | + } |
| 145 | + output.add(maxStart); |
| 146 | + output.add(maxStart+maxLen-1); |
| 147 | + return output; |
| 148 | + } |
| 149 | +} |
0 commit comments