Draft:Difference array: Difference between revisions

Content deleted Content added
Citation bot (talk | contribs)
Removed parameters. | Use this bot. Report bugs. | Suggested by Eastmain | #UCB_webform 458/1621
Heyyo53 (talk | contribs)
Range Queries: Added a proof, maybe not the most substantial one but I will edit later
Line 1:
{{Short description|Computer Science Algorithm}}
{{Draft topics|stem}}
{{AfC topic|stem}}
Line 15:
\end{array}</math>
 
=== Inverse Function ===
A difference array can be undone using a prefix sum array. Here the prefix sum array is denoted as <math>P(c, A)</math> where <math>c</math> is an arbituary constant prepending the prefix sum array. Given that <math>c</math> is <math>A_0</math> by plugging into the prefix sum function <math>P(A_0, D(A))=A</math><ref name=":0" />
 
=== Uniqueness of Difference Arrays ===
<math>A</math> only has a single difference array <math>D(A)</math>. If no additional inputs are given <math>D(A)</math> uses the elements of <math>A</math> to form the difference array. The non-communativity of subtraction only allows for single way to represent a given difference array.<ref name=":0" />
 
=== Range Queries ===
A difference array can be used to update an array that is being modified using range queries in constant time.<ref name=":1">{{Cite web |last=Katiyar |first=Ishank |date=2021-07-30 |title=Understanding Difference Array: The Underrated Constant Time Range Update Algorithm (Part 1) |url=https://medium.com/@ishankkatiyar162/understanding-difference-array-the-underrated-constant-time-range-update-algorithm-part-1-e432ada7f1f5 |url-status=live |access-date=2025-05-20 |website=Medium}}</ref> Here a query <math>(l, r, x)</math> with <math>l, r</math> as the left and right indices of the array to edit and <math>x</math> as the value to add to the elements within <math>[l,r]</math>.<ref>{{Cite web |last=Nadaf |first=Aman |date=2023-02-28 |title=Difference Array Technique |url=https://teckbakers.hashnode.dev/difference-array-technique |access-date=2025-05-20 |website=TeckBakers}}</ref> Difference arrays exhibit a unique property where when modified with a range query only the bounds of said query are modify. So given the range <math>[l,r]</math> the elements of <math>D(A)</math> will remain unchanged except for <math>D(A)[l], D(A)[r]</math> which will be <math>x</math> more than before the query. This allows for a range query to be expressed by <math>D(A)[l]+1</math> and <math> D(A)[r+1]-1</math>.<ref>{{Cite web |last=Nadaf |first=Aman |date=2023-02-28 |title=Difference Array Technique |url=https://teckbakers.hashnode.dev/difference-array-technique |url-status=live |access-date=2025-05-20 |website=TeckBakers}}</ref>
 
To obtain the final array a prefix sum can be performed on <math>D(A)</math>, then when the prefix sum of <math>D(A)</math> is added to <math>A</math> all the queries that were to being applied to <math>A</math> will be performed through a single iteration.<ref name=":1" />
 
=== Proof ===
The relative differences of the values that lie within the range <math>(l,r)</math> will remain unchanged after a range query is performed. However the elements <math>l - 1</math> and <math>r + 1</math> will have their relative differences change. Since each element within <math>[l,r]</math> is increasing by <math>x</math> element <math>l</math> will be <math>x</math> greater than the previous entry, similarly element <math>r</math> will be x less than the next entry in the array.
 
<math>A=[a_{1},a_{2},a_{3},a_{4},a_{5}] \underbrace{\to}_{(2,4,x)} [a_{1},a_{2}+x,a_{3}+x,a_{4}+x, a_{5}]</math>
 
<math>D(A) = [ a_{1}, (a_{2}-a_{1})+x, (a_{3}-a_{2})+x, (a_{4}-a_{3})+x , a_{5}-a_{4} ]</math>
 
<math>\begin{array}{l}
D(A)[2]&=(a_{2}-a_{1})+x \\
D(A)[3]&=(a_{3}-a_{2})+x=(a_{3} - ((a_{2} - a_{1} ) + x ) + x \\
&=a_{3}-( a_{2}-a_{1} )- x + x = a_{3}-a_{2}+a_{1}
\end{array}</math>
 
Thus the middle x cancels out showing that <math>x</math> has no effect on the differences of the middle values.
 
== References ==