Need help to fix bug Problem code:LOSTMAX

all the sample outputs are matching please debug the code as it is not submiting ,wrong answer is showing .

#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;cin>>t;
while(t–)
{
vectorv;
while(1)
{
int s;
cin>>s;
v.push_back(s);
char c=getchar();
if(c!=’ ')
{
break;
}
}

  for(int i=0;i<v.size();i++)
  {
      if(v[i]==(v.size()-1))
      {
          v[i]=0;
      }
  }
  sort(v.begin(),v.end());
  cout<<v[v.size()-1]<<"\n";

}
return 0;
}

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

1 Like
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        vector<int> v;
        while (1)
        {
            int s;
            cin >> s;
            v.push_back(s);
            char c = getchar();
            if (c != ' ')
            {
                break;
            }
        }
        for (int i = 0; i < v.size(); i++)
        {
            if (v[i] == (v.size() - 1))
            {
                v[i] = 0;
            }
        }
        sort(v.begin(), v.end());
        cout << v[v.size() - 1] << "\n";
    }
    return 0;
}

Your Formatted code… I don’t exactly know what are you trying to do… but i think the question would be like… to take inputs for every test case and find the maximum among them or something like that… i don’t know how your while(1) loop will terminate…because it may go into infinite loops or maybe it always wait for input causing runtime or TLE…

As @ssjgz said please provide the question as well as submission link don’t just copy paste raw ugly code…

That’s still wrong, alas:

vector v;

leeme edit it…

BTW Original Problem Link : LOSTMAX Problem - CodeChef

SOLUTION :relieved:

#include<bits/stdc++.h>
using namespace std;

int main()
{
  int T;
  cin>>T;
  while(T--)
  {
    vector<int> v;
    while(1)
    {
      int a;
      cin>>a;
      v.push_back(a);
      char c = getchar();
      if(c != ' ') break;
    }
    sort(v.begin(), v.end());
    int n = v.size() - 1;
    if(v[n] != n) cout<<v[n]<<endl;
    else cout<<v[n-1]<<endl;
  }
}

I will take care of this while submiting other codes in the future

but can you pls tell me why my code was not right as the output was same as given in the sample input/outputs of the original question, thanks for editing the submitted code.