EJMAR21A Editorial

PROBLEM LINK:

Practice Link
Contest Link

Author: Abhishek Kumar
Tester: Shekhar Srivastav
Editorialist: Abhishek Kumar

DIFFICULTY: - CAKEWALK
PREREQUISITES : NONE

PROBLEM SUMMARY:
Difference of inputted number with result of

  • swap(ith position , (l-i+1)th position) for all i=[1,l/2]

HINT
swap in a number of 1st and last , 2nd and 2nd last , so on is nothing but its reverse of number.

2 case arises

  1. if length of number is odd - n/2 swap pair will be there but (n/2)+1 will not , but still swap of (n/2)+1 with itself will not diturb our solution.
  2. if length of number is even- there will be exactly n/2 pairs .
    so in generel approach we can reverse number without thinking about their length because its not going affect the solution.

CPP SOLUTION

using namespace std;
int rev(int n){
    int sums=0;
    while(n!=0) {
        sums=sums*10+n%10;
        n/=10;
        }
    return sums;
}
int main() {
    int tc;
    cin>>tc;
    while(tc--){
        int n;
        cin>>n;
        cout<<n-rev(n)<<"\n";
    }
}