`{"id":"2e97c274-2c05-49cb-8cb0-be603e06805d","name":"Longest Consecutive Sequence Of Elements","description":"1. You are given a number n, representing the size of array a.\r\n2. You are given n numbers, representing elements of array a.\r\n3. You are required to print the longest sequence of consecutive elements in the array (ignoring duplicates).\r\n\r\nNote -> In case there are two sequences of equal length (and they are also the longest), then print the one for which the starting point of which occurs first in the array.","inputFormat":"A number n\r\nn1\r\nn2\r\n.. n number of elements","outputFormat":"Elements of longest sequence of consecutive elements of array in separate lines (ignoring duplicates)","constraints":"1 &lt;= n &lt;= 30\r\n0 &lt;= n1, n2, .. n elements &lt;= 15","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid longestConsecutive(vector<int> &num) {\n\n//write your code here\n\n}\n\nint main()\n{\n vector<int>arr;\n int n;\n cin >> n;\n for (int i = 0 ; i < n; i++) {\n int data;\n cin >> data;\n arr.push_back(data);\n }\n longestConsecutive(arr);\n\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main{\r\n\r\npublic static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n}"},"python":{"code":"import sys\n\ndef longestConsecutive(arr):\n #write your code here\n \ndef main():\n arr = []\n n = int(input())\n for i in range(0,n):\n ele=int(input())\n arr.append(ele)\n \n longestConsecutive(arr)\n \nif __name__ == '__main__':\n main()"}},"points":10,"difficulty":"hard","sampleInput":"17\r\n12\r\n5\r\n1\r\n2\r\n10\r\n2\r\n13\r\n7\r\n11\r\n8\r\n9\r\n11\r\n8\r\n9\r\n5\r\n6\r\n11","sampleOutput":"5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13","questionVideo":"https://www.youtube.com/embed/rb73tdVFjYE","hints":[],"associated":[{"id":"131cd065-5b57-4f4e-ae32-cd5c8d345d9f","name":"What will be the space complexity of this problem if you use Hashmap or HashSet? (Highest frequency character)","slug":"what-will-be-the-space-complexity-of-this-problem-if-you-use-hashmap-or-hashset-highest-frequency-character","type":4},{"id":"1639fc29-ec65-4d49-bec9-a79dbf580871","name":"What will be the output for the given input in \"Longest consecutive sequence\" problem? 10 5 9 1 11 8 6 15 3 12 2","slug":"what-will-be-the-output-for-the-given-input-in-longest-consecutive-sequence-problem-10-5-9-1-11-8-6-15-3-12-2","type":4},{"id":"6200b5c3-8b4b-4409-afc0-d4c2dabbb621","name":"If this problem is solved using Brute Force approach, then what will be the time complexity? (Longest consecutive sequence)","slug":"if-this-problem-is-solved-using-brute-force-approach-then-what-will-be-the-time-complexity-longest-consecutive-sequence","type":4},{"id":"d9960285-253e-47a1-8e91-79a1589fad30","name":"What happens if we put a key object in a HashMap which exists? (Longest consecutive sequence)","slug":"what-happens-if-we-put-a-key-object-in-a-hashmap-which-exists-longest-consecutive-sequence","type":4},{"id":"e03c57d0-bb55-49f7-918e-c0dfdc6ae3d4","name":"Which data structure can be used to solve this problem in O(n) time complexity? (Highest frequency character)","slug":"which-data-structure-can-be-used-to-solve-this-problem-in-o-n-time-complexity-highest-frequency-character-14864","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1254d21e-2209-40bc-9e24-00d135ace68d","name":"Hashmap And Heap For Beginners","slug":"hashmap-and-heap-for-beginners","type":0},{"id":"486f5f99-d0cf-4fb0-962f-6b16b61d5438","name":"Longest Consecutive Sequence Of Elements","slug":"longest-consecutive-sequence-of-elements","type":1}],"next":{"id":"e33cd1fc-dc2e-4aa5-ac58-41eec1df75b3","name":"Longest consecutive sequence","type":3,"slug":"longest-consecutive-sequence"},"prev":{"id":"61ba3f11-6c62-476c-9f17-5ad8b71d6804","name":"Get Common Elements-2","type":3,"slug":"get-common-elements-2"}}}`

# Longest Consecutive Sequence Of Elements

1. You are given a number n, representing the size of array a. 2. You are given n numbers, representing elements of array a. 3. You are required to print the longest sequence of consecutive elements in the array (ignoring duplicates). Note -> In case there are two sequences of equal length (and they are also the longest), then print the one for which the starting point of which occurs first in the array.

`{"id":"2e97c274-2c05-49cb-8cb0-be603e06805d","name":"Longest Consecutive Sequence Of Elements","description":"1. You are given a number n, representing the size of array a.\r\n2. You are given n numbers, representing elements of array a.\r\n3. You are required to print the longest sequence of consecutive elements in the array (ignoring duplicates).\r\n\r\nNote -> In case there are two sequences of equal length (and they are also the longest), then print the one for which the starting point of which occurs first in the array.","inputFormat":"A number n\r\nn1\r\nn2\r\n.. n number of elements","outputFormat":"Elements of longest sequence of consecutive elements of array in separate lines (ignoring duplicates)","constraints":"1 &lt;= n &lt;= 30\r\n0 &lt;= n1, n2, .. n elements &lt;= 15","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid longestConsecutive(vector<int> &num) {\n\n//write your code here\n\n}\n\nint main()\n{\n vector<int>arr;\n int n;\n cin >> n;\n for (int i = 0 ; i < n; i++) {\n int data;\n cin >> data;\n arr.push_back(data);\n }\n longestConsecutive(arr);\n\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main{\r\n\r\npublic static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n}"},"python":{"code":"import sys\n\ndef longestConsecutive(arr):\n #write your code here\n \ndef main():\n arr = []\n n = int(input())\n for i in range(0,n):\n ele=int(input())\n arr.append(ele)\n \n longestConsecutive(arr)\n \nif __name__ == '__main__':\n main()"}},"points":10,"difficulty":"hard","sampleInput":"17\r\n12\r\n5\r\n1\r\n2\r\n10\r\n2\r\n13\r\n7\r\n11\r\n8\r\n9\r\n11\r\n8\r\n9\r\n5\r\n6\r\n11","sampleOutput":"5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13","questionVideo":"https://www.youtube.com/embed/rb73tdVFjYE","hints":[],"associated":[{"id":"131cd065-5b57-4f4e-ae32-cd5c8d345d9f","name":"What will be the space complexity of this problem if you use Hashmap or HashSet? (Highest frequency character)","slug":"what-will-be-the-space-complexity-of-this-problem-if-you-use-hashmap-or-hashset-highest-frequency-character","type":4},{"id":"1639fc29-ec65-4d49-bec9-a79dbf580871","name":"What will be the output for the given input in \"Longest consecutive sequence\" problem? 10 5 9 1 11 8 6 15 3 12 2","slug":"what-will-be-the-output-for-the-given-input-in-longest-consecutive-sequence-problem-10-5-9-1-11-8-6-15-3-12-2","type":4},{"id":"6200b5c3-8b4b-4409-afc0-d4c2dabbb621","name":"If this problem is solved using Brute Force approach, then what will be the time complexity? (Longest consecutive sequence)","slug":"if-this-problem-is-solved-using-brute-force-approach-then-what-will-be-the-time-complexity-longest-consecutive-sequence","type":4},{"id":"d9960285-253e-47a1-8e91-79a1589fad30","name":"What happens if we put a key object in a HashMap which exists? (Longest consecutive sequence)","slug":"what-happens-if-we-put-a-key-object-in-a-hashmap-which-exists-longest-consecutive-sequence","type":4},{"id":"e03c57d0-bb55-49f7-918e-c0dfdc6ae3d4","name":"Which data structure can be used to solve this problem in O(n) time complexity? (Highest frequency character)","slug":"which-data-structure-can-be-used-to-solve-this-problem-in-o-n-time-complexity-highest-frequency-character-14864","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1254d21e-2209-40bc-9e24-00d135ace68d","name":"Hashmap And Heap For Beginners","slug":"hashmap-and-heap-for-beginners","type":0},{"id":"486f5f99-d0cf-4fb0-962f-6b16b61d5438","name":"Longest Consecutive Sequence Of Elements","slug":"longest-consecutive-sequence-of-elements","type":1}],"next":{"id":"e33cd1fc-dc2e-4aa5-ac58-41eec1df75b3","name":"Longest consecutive sequence","type":3,"slug":"longest-consecutive-sequence"},"prev":{"id":"61ba3f11-6c62-476c-9f17-5ad8b71d6804","name":"Get Common Elements-2","type":3,"slug":"get-common-elements-2"}}}`

Editor

# Longest Consecutive Sequence Of Elements

hard

1. You are given a number n, representing the size of array a. 2. You are given n numbers, representing elements of array a. 3. You are required to print the longest sequence of consecutive elements in the array (ignoring duplicates). Note -> In case there are two sequences of equal length (and they are also the longest), then print the one for which the starting point of which occurs first in the array.

## Constraints

1 <= n <= 30 0 <= n1, n2, .. n elements <= 15

## Format

### Input

A number n n1 n2 .. n number of elements

### Output

Elements of longest sequence of consecutive elements of array in separate lines (ignoring duplicates)

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}17 12 5 1 2 10 2 13 7 11 8 9 11 8 9 5 6 11```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}5 6 7 8 9 10 11 12 13```

Question Video

Discussions

Show Discussion

Related Resources