my code is going well in some test cases but failing in some of the other test cases …what is wrong with my code
my code-----
#include
using namespace std;
int main() {
int x,k;
cin>>x;
k=x;
int n=0;
while(k)
{
k=k/10;
n++;
}
int a[n];
for(int i=n-1;i>=0;i–)
{
a[i]=x%10;
x=x/10;
}
for(int i=0;i<n;i++)
{
if(9-a[0]==0)
continue;
if(a[i]>9-a[i])
a[i]=9-a[i];
}
for(int i=0;i<n;i++)
cout<<a[i];
}
Wrong answer in some test cases
this is my code if it is not readible in doubt section
#include using namespace std; int main() { int x,k; cin>>x; k=x; int n=0; while(k) { k=k/10; n++; } int a[n]; for(int i=n-1;i>=0;i–) { a[i]=x%10; x=x/10; } for(int i=0;i<n;i++) { if(9-a[0]==0) continue; if(a[i]>9-a[i]) a[i]=9-a[i]; } for(int i=0;i<n;i++) cout<<a[i]; }
The number shouldn’t contain leading zeroes.
for 9999 your code give 0000 but ans should be 0.
my code is failing 3 out of 5 test cases, is this the only problem with my code
have you consider the case of leading zeroes
i have considered the zeros then what is the problem
Hello @sawan_verma,
In this question, you have to find and print the smallest number possible by reversing digits of the input.
-
As x <= 100000000000000000, use long long int data type or for this specific question, you can use a string also as it is easier to iterate on the digit of the number.
-
Now starting from the leftmost digit in the number iterate till the rightmost digit, do the following:
2.1. find the invert of the current digit s[i] i.e. 9-s[i]
2.2. check if the invert is smaller than the actual digit:
…if yes, then replace the digits[i] in the number with its invert.
…if no, then don’t replace
example 1:
123
inverting 1: 9-1=8
as 8>1,
so, if I would replace 1 with 8 (i,e 823), then the number will become larger.
But, we are required to find the smallest, so don’t replace.
example 2:
632
inverting 6: 9-6=3
as 3<6,
so, if I would replace 6 with 3 (i,e 332), then the number will become smaller.
Thus, replace will be made. -
As it is mentioned that the leftmost digit can’t be 0, so we would not replace if a[0]=9
example:
989
inverting 9: 9-9=0
0<9,
But, the number cannot start with 0.
So, we won’t replace.
Output:
910
Hope, this would help.
Give a like, if you are satisfied.
i have implemented the exact same logic but my code is failing in 2 test cases
Hello @sawan_verma
There are two issues with your code:
Please read point 1 and point 3 of my last reply carefully and correct your code accordingly.
- Modify the datatype.
- You are skipping all 9 which is wrong:
if you see the example that I have explained in the third point:
for 989, answer is 910
Similarly, for 999, the answer is 900
Hope, this would help.
Give a like, if you are satisfied.
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.