I have understood the problem but I am not able to understand the code through the video.
Can you please explain me the code.
Chewbacca and Number, Hackerblocks Tutorial
Hey @avijuneja2007
In the code, weβre storing the number in a character array
Let me explain the parts of the code:
int i = 0;
if(a[i] == '9'){
i++;
}
See basically if first digit is 9(the first element of the string is β9β), we donβt have to invert it coz answer canβt start with a zero. (we donβt have to reduce no of digits in the given number).
Now for the further digits, we invert them(subtract the digit from 9) if they are greater than 5, coz weβve to find the minimum possible number. and inverting the digits larger than 5 will result in smaller digit and hence smaller resultant number.
for( ; a[i]!=β\0β; i++){
int digit = a[i]-β0β;
if(digit >= 5){
digit = 9-digit;
a[i] = digit+β0β;
}
}
So for every element of the array, we find the digit from the character by subtracting β0β (coz we need int and β3β-β0β will give us 3). Then we invert the digit if itβs greater than 5, and then convert the digit back to character(β0β+3 will give us β3β) and store it in the array.
So, basically what you are trying to say is that if we take the a[i] as 5 , then according to ASCII table , the value corresponding to the char 5 is 53 and we need to subtract char 0 from it having a corresponding value of 48 inorder to convert it into int. Like β5β-β0β means 53-48? Am I saying right?
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.