Programming language
A programming is actually a set of instruction which we are actually giving to machine and it perform their task according to that....
1. Best solution takes O(n) time.
2. If two sequences are of equal length, then return the sequence starting with the number whose occurrence is earlier in the array.
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.
The following line contains n space separated integers, that denote the value of the elements of the array.
The first and only line of output contains starting and ending element of the longest consecutive sequence. If the length of the longest consecutive sequence is 1, then just print the starting element.
0 <= n <= 10^6
Time Limit: 1 sec
13
2 12 9 16 10 5 3 20 25 11 1 8 6
8 12
7
3 7 2 1 9 8 41
7 9
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.
7
15 24 23 12 19 11 16
15 16
/*vector<int> longestConsecutiveIncreasingSequence(int *arr, int n) { // Your Code goes here unordered_map<int, int> m; vector<int> ans; for (int i = 0; i < n; i++) m[arr[i]] = i; for (int i = 0; i < n; i++) { vector<int> temp; // Find the starting point for the increasing sequence if (m.count(arr[i] - 1) == 0) { temp.push_back(arr[i]); int t = 1; while (m.count(arr[i] + t)) { temp.push_back(arr[i] + t); t++; } if (temp.size() > ans.size()) ans = temp; } } int n1 = ans[0]; int n2 = ans[ans.size() - 1]; ans.clear(); ans.push_back(n1); ans.push_back(n2); return ans;}*/#include<vector>#include<unordered_map>vector<int> longestConsecutiveIncreasingSequence(int *arr, int n) { unordered_map<int,int> ourmap; for(int i = 0;i < n;i++) { ourmap[arr[i]]++; } int max = 0; int startIndex; for(int i = 0; i < n;i++) { int cnt = 0; int j = 0; while(ourmap.count(arr[i] + j ) != 0) { cnt++; j++; } if(cnt> max) { max = cnt; startIndex = arr[i]; } } vector<int> ans; ans.push_back(startIndex); ans.push_back(startIndex + max-1 ); return ans;}
Comments
Post a Comment