Split an integer into two parts to minimize sum

Revision en1, by typedeftemplate, 2021-09-12 04:36:59

Given $$$N \leq 10^{9}$$$, I want to place a vertical bar between any two consecutive digits to form two new integers. Let's call them $$$X$$$ and $$$Y$$$. How can we minimize $$$|X - Y|$$$?

Clearly, $$$N$$$ is so large, so we must get some non-linear solution. I thought about implementing something in which we first and second part, and we manually remove and add one digit at a time. However, this seems to be a little bit difficult when there are zeros.

For example, consider $$$30001$$$. We can split at the first zero to get $$$3$$$ and $$$0001$$$, so the answer is $$$|3 - 1| = 2$$$.

I also thought about using DP, but I think even a linear solution would get TLE. Any ideas are greatly appreciated.

Tags #implementation, #basic math

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English typedeftemplate 2021-09-12 04:36:59 730 Initial revision (published)