Skip to content

Instantly share code, notes, and snippets.

@janvichhabra
Last active October 24, 2021 13:18
Show Gist options
  • Save janvichhabra/a48d96914127451858810f995c9b9e75 to your computer and use it in GitHub Desktop.
Save janvichhabra/a48d96914127451858810f995c9b9e75 to your computer and use it in GitHub Desktop.
pepcoding.com 21/10/21 longestConsecutiveSequenceOfElements
#include<bits/stdc++.h>
using namespace std;
void longestConsecutive(vector<int> &num) {
unordered_map<int,bool>mp;
for (int i=0;i<num.size();i++){
mp[num[i]]=true;
}
for (int i=0;i<num.size();i++){
if(mp.find(num[i]-1)!=mp.end()){
mp[num[i]]=false;
}
}
int msp = 0;
int ml = 0;
for (int val : num) {
if(mp[val] ==true){
int tsp = val;
int tl = 1;
while(mp.find(tsp + tl)!=mp.end()){
tl++;
}
if(tl > ml){
ml = tl;
msp = tsp;
}
}
}
for(int i = 0; i < ml; i++){
cout<<msp + i<<endl;
}
}
int main()
{
vector<int>arr;
int n;
cin >> n;
for (int i = 0 ; i < n; i++) {
int data;
cin >> data;
arr.push_back(data);
}
longestConsecutive(arr);
}
import sys
def longestConsecutive(arr):
thisdict={}
for i in range(len(arr)):
thisdict[arr[i]]="true"
msp=0
m1=0
for i in range (len(arr)):
val=arr[i]
t1=1;
while thisdict.get(val+t1) is not None:
t1=t1+1
if t1>m1:
m1=t1
msp=val
for i in range(0,m1):
print(msp+i)
def main():
arr = []
n = int(input())
for i in range(0,n):
ele=int(input())
arr.append(ele)
longestConsecutive(arr)
if __name__ == '__main__':
main()
#include<bits/stdc++.h>
using namespace std;
void longestConsecutive(vector<int> &num) {
//write your code here
}
int main()
{
vector<int>arr;
int n;
cin >> n;
for (int i = 0 ; i < n; i++) {
int data;
cin >> data;
arr.push_back(data);
}
longestConsecutive(arr);
return 0;
}
import sys
def longestConsecutive(arr):
#write your code here
def main():
arr = []
n = int(input())
for i in range(0,n):
ele=int(input())
arr.append(ele)
longestConsecutive(arr)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment