Not sure what is wrong

#include<bits/stdc++.h>
using namespace std;
vector split (const string &s, char delim) {
vector result;
stringstream ss (s);
string item;

while (getline (ss, item, delim)) {
    result.push_back (stoi(item));
}

return result;

}
int main() {
string input;
cin>>input;
string s = input.substr(1,input.length()-2);
map<int,vector> m;
vector v = split (s, ‘,’);
for(int i=0;i<v.size();i++){
m[v[i]].push_back(i);
}
int ans=2;
for(int i=0;i<v.size()-1;i++){
for(int j=i+1;j<v.size();j++){
int num1 = v[i];
int num2 = v[j];
int diff = num2-num1;
int pos = j;
int next_num = num2+diff;
int count = 2;
while(m.find(next_num)!=m.end()){
vectortemp = m[next_num];
bool flag = false;
for(int k=0;k<temp.size();k++){
if(pos<temp[k]){
pos=temp[k];
next_num = v[pos]+diff;
count++;
flag = true;
break;
}
}
if(!flag){
break;
}
}
ans = max(ans,count);
}
}
cout<<ans<<endl;

return 0;

}

Hey @varss777
Debug for this [23, 2, 4, 6, 7]
answer is 3

this [23,2,4,6,7] is giving answer as 3

Please share ur code in CB IDE
I was using ur last submitted code and it was giving 2

do not use extra space after comma i.e. instead of this [23, 2, 4, 6, 7] use [23,2,4,6,7]

So here is the error
In input file there are spaces in testcases as well

Instead of this do
getline(cin,input);

this -> string input; cin>>input; wil fail. Can you please tell how to take input in this case.

cin>>input will take input until a space or next line is not encountered
whereas getline(cin,input) will take input until next line is not encountered
Hence we used getline(cin,input) here to take input

thanks , it worked now!

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.