question-
https://online.codingblocks.com/app/player/246132/content/235737/4713/code-challenge
Not passing few testcases
#include using namespace std; long long rem=0,sum=0; long long invert(long long x ){ while(x!=0){ rem=x%10; x=x/10; int t=9-rem; if(rem>t){ sum=sum10+t; } else{ sum=sum10+rem; } } return sum; } int back(long long sum){ long long x=0; while(sum!=0){ long long k=sum%10; sum=sum/10; x=x*10+k; } return x; } int main(){ long long x; cin>>x; invert(x); cout<<back(sum); } This is the code
hi @sbhardwaj1be21_2a991b329433c059, send the code in ide.codingblocks.com
the first code u shared is different
It is showing showing 0 as the output…how do we solve this?
hi @sbhardwaj1be21_2a991b329433c059 refer
if digit is >5 flip that digit otherwise don’t flip
also check some corner cases
a) first digit should not be zero so if first digit is 9 then it should remain unchanged
b) if number is 0 only then output should be 9, not 0.
The logic behind this Problem was pretty simple as we can invert any digit ( 9 - digit) but we need to invert only such digits that will eventually end up giving the smallest number possible.
So, we should invert only digits greater then or equal to 5 as after inverting them, the result gives us smallest number.
For e.g.,
9 - 5 = 4 viz, smaller than the original number that was 5.
9 - 8 = 1 viz, smaller than the original number that was 8.
but 9 - 1 = 8 viz, greater than the original number that was 1.
Important point to consider is, After inverting any digits their should not be trailing zeros that means, if their is 9 at the starting of the number then it must remain the same
Also input can be quite big so, u need to capture the number in long long int in c++ and long in java.
for implementation difficulties refer https://ide.codingblocks.com/s/658154