CDIT03 - Editorial

PROBLEM LINK:

Practice

Contest

Author: Hardik Singh Negi

Tester: Amandeep Singh

Editorialist: Akshay Sharma

DIFFICULTY:

EASY-MEDIUM

PREREQUISITES:

Arrays

PROBLEM:

That computer gives a token, which consists of space separated elements. The
elements are numbers each ranging from 0 to 9. The encryption can be broken
by constructing a number with the given elements in the order given by the computer and
adding the reverse of that number to the original number. The sum so obtained is the key
to break the encryption.

QUICK EXPLANATION:

The tokens given by the computer is joined and formed a number. The number is then reversed and added with the original number. The sum is the answer to the problem.

EXPLANATION:

Initialize a variable with first input which is the number of test cases.

For each test case we initialized sz equal the the size given by next line input.

Now we store every element one by one in an array. The array now has the number formed by joining the numbers.

Now, we add first element by last, second element by second last and so on using a loop. Here, we must have a variable that takes care of the carry value and pass it on to next iteration for addition of next digits. We must also check if the sum we get by adding two numbers is single digit or double, if it is single we leave it as it is and carry is 0, but if it is double, we make first digit as carry and second as the sum.

We store this result in an array which starts filling in reverse order as we add from backward.
Finally our array has the desired result, the key to break the code, print it and help Indian Intelligence.

AUTHOR’S AND TESTER’S SOLUTIONS:

Author’s solution can be found here.

Tester’s solution can be found here.

#include<stdio.h>

int main(int argc, char const *argv[])

{

int n;

scanf("%d",&n);

int a[n],b[n],c[n];

for (int i = 0; i <n ; i++)

{

    scanf("%d",&a[i]);

}

for (int j = 0; j <n; j++)

{

    b[j]=a[n-1-j];

}

for (int k = n-1; k >=0; k--)

{

    if (a[k]+b[k]>=10 )

    {

       if (k==n-1)

       {

          c[k]=a[k]+b[k]-10;

       }

      else if (k<n-1)

      {

          c[k]=a[k]+b[k]-10+1;

          if (k==0)

          {

             c[k]=a[k]+b[k]+1; 

          }

          

      }

}

    else if (a[k]+b[k]<10)

    {

        c[k]=a[k]+b[k];

    }

    

}

for (int l = 0; l < n; l++)

{

    printf("%d", c[l]);

}



return 0;

}