0% found this document useful (0 votes)
10 views5 pages

Feb 4

Uploaded by

Harshit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Feb 4

Uploaded by

Harshit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Difficulty Level: Medium

Question / Problem Statement


Ronaldo is studying in the third grade. During the last math lesson all the pupils wrote
on arithmetic quiz. Ronaldo is a clever boy, so he managed to finish all the tasks pretty
fast, and Messi gave him a new one, that is much harder.

Let's represent the flip operation of an integer as follows: The number is considered in
decimal notation and then returned. If there are any subsequent leading zeros, they are
discarded. For example, if we flip 123 the result is the integer 321, but flipping 130 we
get 31, and flipping 31 we get 13.

Messi picked some number a without leading zeroes, and flipped it to get number a r.
Then she summed a and ar, and told Ronaldo the resulting value n. His goal is to find
any valid a.

As Messi picked some small integers as a and ar, Ronaldo managed to find the answer
pretty fast and became interested in finding some general algorithm to deal with this
problem. Now, he wants you to write the program that for given n finds any a without
leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Function Description
In the provided code snippet, implement the provided solve(...) method using the
variables to print a without leading zeroes, such that a + ar = n or determine that such a
doesn't exist. You can write your code in the space below the phrase “WRITE YOUR
LOGIC HERE”.
There will be multiple test cases running so the Input and Output should match exactly
as provided.
The base Output variable result is set to a default value of -404 which can be modified.
Additionally, you can add or remove these output variables.

Input Format
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Sample Input
4

Constraints
1 ≤ n ≤ 10100000
Output Format
If there is no such positive integer a without leading zeroes that a + ar = n then print 0.
Otherwise, print any valid a. If there are many possible answers, you are allowed to pick
any.
Sample Output
2
Explanation
In the first sample 4 = 2 + 2, a = 2 is the only possibility.

In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is


incorrect, because a can't have leading zeroes.

It's easy to check that there is no suitable a in the third sample.

In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a
= 12, a = 21. Any of these is considered to be correct answer.
Solution Steps:
Lets say that input has length of n digits, then size of answer can be n if we didn't carry 1 to the
left out of addition, and n - 1 otherwise. Lets fix length m of our answer and denote i-th number
in the representation as ai. Then we know from the rightmost digit of the sum. Lets figure out
what does equals to. If the remainder is 9, it means that, because we can't get 19 out of the sum
of two digits. Otherwise, the result is defined uniquely by the fact that there was carrying 1 in the
leftmost digit of the result or not. So, after this we know a1 + am. It doesn't matter how we divide
sum by two digits, because the result will be the same. After this we can uniquely identify the
fact of carrying after the first digit of the result and before the last digit. Repeating this m / 2
times we will get candidate for the answer. In the end we will have O(n) solution.

If you've missed the fact that every step is uniquely defined, then you could've wrote basically
the same solution, but with dynamic programming.
Running Solution in C++:
#include <iostream>
using namespace std;
char s[100005];
string t;
int main(){
cin >> t;
int n = t.size(), l = 1, r = n;
for(int i = 1; i <= n; i++){
s[i] = t[i - 1] - '0';
}
if(s[l] != s[r]){
s[l]--;
s[l + 1] += 10;
if(s[l] == 0) l++;
}
while(l <= r){
if(s[l] != s[r]){
if ((s[l]-s[r] == 10) || (s[l]-s[r]==11)&&s[r]<10){
s[r-1]--;
s[r]+=10;
}
if (s[l]-s[r]==1){
s[l]--;
s[l+1]+=10;
}
}
if (s[l]!=s[r]){
printf("0");
return 0;
}
if (l!=r){
s[l]=s[l]-(s[r]/2);
s[r] /= 2;
}
else
if (((s[l] / 2) * 2)==s[l])
s[l] /= 2;
else{
printf("0");
return 0;
}
if (s[l]>9||s[l]<0||s[r]>9||s[r]<0){
printf("0");
return 0;
}
l++;
r--;
}
l = 1;
if(s[l] == 0)
l++;
while(l<=n){
printf("%d",s[l]);
l++;
}
return 0;
}

Input:
4
Output:
2
Test Cases :

Test Case Input Output


No
1 4 2
2 99 54
3 101 100
4 121 110
5 161 130
6 165 87
7 686686 343343
8 928818 469854
9 30092 15541
10 33 21
11 5 0
12 11 10

Code stubs in C++:


#include <iostream>
using namespace std;
int solve(int n){
int result=-404;
// Write Your logic here

return result;
}
int main() {
int n;
cin>>n;
cout<<solve(n);
return 0;
}

You might also like