Smart Phone (ZCO14003)

The following is the solution to a problem named " Smart Phone" which runs successfully on vs code but prompt a runtime error “SIGSEGV” on codechef IDE kindly help me out …

#include<bits/stdc++.h>
using namespace std;
int main()
{

 long  numberOfPotentials=0;
 cin>>numberOfPotentials;
 vector<long long> budget(numberOfPotentials,0);
 for(int i=0;i<budget.size();i++){
     cin>>budget[i];
 }

 long long avgBudget=0;
 for(int i=0;i<budget.size();i++)
 {
     avgBudget+=budget[i];
 }
 avgBudget/=numberOfPotentials;
 sort(budget.begin(),budget.end());
 auto pos= lower_bound(budget.begin(),budget.end(),avgBudget);
 long long price = *pos;
 int m=(budget.end()-pos);
 cout<<price*m;

return 0;
}

value of auto pos= lower_bound(budget.begin(),budget.end(),avgBudget); can be budget.end() if the last element in the vector is less than avgBudget. I think this is causing SIGSEGV error.

Before
auto pos= lower_bound(budget.begin(),budget.end(),avgBudget);
you should check if the lower bound is present by an if statement as,

auto pos= lower_bound(budget.begin(),budget.end(),avgBudget);

             if( pos != budget.end() ){
                           //Code 
                    }
             else{
                         //
                  }

thanks , that worked .
But the resolved code worked as a wrong answer.