Last active
November 15, 2016 07:22
-
-
Save yaojingguo/beb63038a7183d5aed5ee88cedb2c889 to your computer and use it in GitHub Desktop.
math proof for METHOD 2 in http://www.geeksforgeeks.org/two-elements-whose-sum-is-closest-to-zero/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\documentclass{article} | |
\usepackage{amsmath, amssymb, amsthm} | |
\begin{document} | |
The invariant is that $min\_sum$ is the min-abs-sum of pairs whose one element | |
is among $a[0]...a[l-1]$ or $a[r+1]...a[n-1]$. | |
\textbf{Initialization}: Before the first iteration, $l = 0$ and $r = n- 1$. | |
$a[0]...a[-1]$ or $a[n]...a[n-1]$ is empty. It is trivial to see that | |
the invariant is true. | |
\textbf{Maintenance}: If $sum > 0$, the min-abs-sum which involves $a[r]$ and | |
does not involve elements among $a[0]...a[l-1]$ or $a[r+1]...a[n-1]$ is $sum$ | |
since $sum \ge a[i] + a[r] (l < i < r)$. So picking one of $min\_sum$ and $sum$ | |
with the less absulate value as $min\_sum$ ensures that $min\_sum$ is the | |
min-abs-sum of pairs whose one element is among $a[0]...a[l-1]$ or | |
$a[r]...a[r]$. Before the next iteration, $r$ is increased by $1$. So the | |
invariant holds. The case for $sum < 0$ is symmetric. | |
\textbf{Termination}: Before the iteration for $l = r$, $min\_sum$ is the | |
min-abs-sum of pair whose one element is among $a[0]...[l-1]$ or | |
$a[l]...a[n-1]$ which is $a[0]...a[n]$. | |
\end{document} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment