I have compiled your code in two of my compiler and changed its header files it works fine for me
PS I changed #include <bits/stdc++.h>
header file with #include<vector>
and #include <unordered_set>
. Moreover I added { }
currly braces in if statement because sometimes compilers as well as editors gets confuse if we dont use currly braces with loops and conditional statement.
Here is your code
#include <iostream>
#include<vector>
#include <unordered_set>
using namespace std;
int main()
{
vector<int> v;
unordered_set<int> s;
int count, res = 0;
cout << "Enter the elements of the array: ";
while (1)
{
cin >> count;
if (count == -1)
{
break;
v.push_back(count);
s.insert(count);
}
}
for (int i = 0; i != v.size(); i++)
{
if (s.find(v.at(i) - 1) != s.end())
{
count = 1;
while (s.find(v[i] + 1) != s.end())
count++;
res = max(res, count);
}
}
cout << res;
return 0;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…