Help me optmize

help me optimize
#include
using namespace std;
int main(){
long long int x;
cin>>x;
int i;
long long int dt,ans=0;
long long int pv=1;
for(i=1;x!=0;i++){
dt=x%10;
ans+=min(dt,9-dt)pv;
x/=10;
pv=pv
10;
}
pv=1;
for(int j=1;j<=i-2;j++){
pv=pv10;
}
if(ans/pv==0){
ans=ans+9
pv;
}
cout<<ans;
return 0;
}

Hello @sakshamngplsn_8a4528baa287a8ff,

Just add a small condition whether dt==9 and x == 0 or not like this:

#include <iostream>

using namespace std;

int main(){

    long long int x;

    cin>>x;

    int i;

    long long int dt,ans=0;

    long long int pv=1;

    for(i=1;x!=0;i++){

        dt=x%10;

        ans+=min(dt,9-dt)*pv;

        x/=10;

        if(x == 0 && dt==9){

            ans += dt*pv;

        }

        pv=pv*10;

    }

    cout<<ans;

    return 0;

}

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.