#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;
}
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.
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!