I was doing a problem where you need to find the best K figures to eliminate from N so the number would have the minimum difference between the smallest and highest figure.
#include<bits/stdc++.h>
using namespace std;
int diff(vector <int> x){
sort(x.begin(),x.end());
return x.back()-x[0];
}
int diffMin(vector<int> x,int h,int best){
if(h==0) return diff(x);
else{
for(int a=0;a<x.size();a++){
int y=x[a];
x.erase(x.begin()+a);
best=min(best,diffMin(x,h-1,best));
x.insert(x.begin()+a,y);
}
return best;
}
}
int main(){
int n,k;
cin>>n>>k;
vector <int> x;
while(n!=0){
x.push_back(n%10);
n/=10;
}
cout<<diffMin(x,k,10);
}
I put it in an evaluator but it said "Caught fatal signal 11(SigSegV )". Could someone explain me why?