Help in HELPTECH (Code Mantra)

if anyone wants a c++ code…

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t,n;
cin>>t;
while(t–)
{
cin>>n;
string s;
ll ph[n],marks[n];
float sum=0;
vector<pair<string,ll>>v;
for(int i=0;i<n;i++)
{
cin>>s>>ph[i]>>marks[i];
v.push_back(make_pair(s,marks[i]));
sum+=marks[i];
}
sum=sum/(double)n;
sort(marks,marks+n);
for(int i=0;i<n;i++)
{
if(marks[i]<sum)
{
for(int j=0;j<n;j++)
{
if(v[j].second==marks[i])
{
cout<<v[j].first<<" “<<ph[j]<<” "<<v[j].second<<endl;
v[j].second=-1;
}
}
}
}
}
return 0;
}

You are getting sorted output by string.
Because you are not converting them to int before sorting.
For example,

final = [ ['baban','1234567890','44'] ,['abhichal','9876543210','5'] ]
ans=sorted(final,key=itemgetter(2))

Then ans = [ [‘abhichal’,‘9876543210’,‘44’], [‘baban’,‘1234567890’,‘5’]]. Because 44 is alphabetically smaller then 5.

However, if you write

ans=sorted(final,key=int(itemgetter(2)))

You will get it correctly.

ohk got it

would be nice if you format your codes… (use ``` for basics or visit the discuss page)

thanks : )

@babangain @nuttela Can you tell me why the following source-code is giving WA: https://paste.ubuntu.com/p/ndSXVDxqqn/

Simply use a comparator sort in C++

Code

Thanks,
will sure remember from next time :smiley:

1 Like

welcome :wink: