# How to solve this...? I tried but couldnt solve

Contest Code:PRACTICE

Problem Code:FLOW004

Why is my code failing for some hidden test cases ?

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
if(n < 10)
{
cout<<n<<endl;
}
else
{
int last_dig = n % 10;
int first_dig = 0;
while(n)
{
first_dig = n % 10;
n = n / 10;
}
cout<<(last_dig+first_dig)<<endl;
}
}
return 0;
}

can u help me find that corner case ?

it should be

``````t-- //  Try doing that
``````

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t- -) // It Should Be t- -
{
int n;
cin>>n;
if(n < 10)
{
cout<<n<<endl;
}
else
{
int last_dig = n % 10;
int first_dig = 0;
while(n)
{
first_dig = n % 10;
n = n / 10;
}
cout<<(last_dig+first_dig)<<endl;
}
}
return 0;
}

ya in my code its actually t–
still not working…

its t-- in my code

still its not working

https://www.codechef.com/viewsolution/41971263

Check this is my solution . what iam doing is just storing all the digits of number in an array or an vector and printing sum of first and last element of that array which is our required answer.

Condition for n < 10 is wrong
You should print (n+n) not (n)

why it should be (n+n)

suppose the number n = 5;
then the sum of its first and last digits it 5 …not 10 ??

could you explain that to me ?

ya…thats correct…

but if we wish to do it without any extra space…

For constant space, check if the given number is a single digit number by `if(n % 10 == 0)` . If it is `true`, the print `2*n`, else do what you’ve done earlier and then print the answer.

But the vector method is also constant space as constraints over N is just 10^6.
Hope it helped, happy coding!

You must know that floor(1+log10(n)) gives the number of digits in n.

Let the above value be p.

E.g. if n=12345 then p will be 5.

Then, to extract the first digit you have to do n/pow(10,p-1).

e.g. 12345/10000 = 1

So, sum of first and last digits will be n/pow(10,p-1) + n%10

O(1) time and O(1) space complexities

In case of single digit numbers (ex. n = 5), the first digit is also the last digit. Hence answer is 10 (5+5).

yaa correct.

thanks