Draft:Difference array
{{Short description|Computer Science Algorithm}}
{{Draft topics|stem}}
{{AfC topic|stem}}
{{AfC submission|||ts=20250523233053|u=Heyyo53|ns=118}}
{{AFC submission|d|nn|u=Heyyo53|ns=118|decliner=ToadetteEdit|declinets=20250521084736|ts=20250520231757}}
An array that is constructed using a sequence of numbers and the differences between each element forming a new array in which . The difference array of can be denoted as . The main goal of a difference array is to show the amount of change between consecutive elements in an array.{{Cite book |last=Laaksonen |first=Antti |url=https://www.google.com/books/edition/Guide_to_Competitive_Programming/3JbiDwAAQBAJ?hl=en&gbpv=0 |title=Guide to Competitive Programming: Learning and Improving Algorithms Through Contests |date=2020-05-08 |publisher=Springer Nature |isbn=978-3-030-39357-1 |publication-date=2020-05-08 |pages=137 |language=en}}{{Cite web |title=Prefix sum array and difference array - PEGWiki |url=https://wcipeg.com/wiki/Prefix_sum_array_and_difference_array |access-date=2025-05-20 |website=wcipeg.com}}
y_{0}=x_{0} \\
y{1}=x_{1}-x_{0} \\
\vdots \\
y_{n}=x_{n}-x_{n-1} \\
\end{array}
Properties
= Inverse Function =
= Uniqueness of Difference Arrays =
Usage
= Range Queries =
A difference array can be used to update an array that is being modified using range queries in constant time.{{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}} Often in the context of range queries the difference array is initially set to an array of 0's. Here a query with as the left and right indices of the array to edit and as the value to add to the elements within . 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 the elements of will remain unchanged except for which will be more than before the query. This allows for a range query to be expressed by and .{{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}}
\begin{array}{l}
D(A)[l] = 1 \\
D(A)[r+1] = -1 \\
\end{array}
By performing a prefix sum on , once added to in where each matching index is added to the others, the resulting array will be as if each query was performed. This allows for any number of queries to to be performed within a single iteration.
== Proof ==
The relative differences of the values that lie within the range will remain unchanged after a range query is performed. However the elements and will have their relative differences change. Since each element within is increasing by element will be greater than the previous entry, similarly element will be x less than the next entry in the array.
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}
Thus the middle x cancels out showing that has no effect on the differences of the middle values.
= Steganalaysis =
Methods of JPEG base steganography can be detected using difference arrays. It has been shown that Markov features that were extracting from zigzag intra-block and inter-block difference array improve steganography detection substantially. By calculating difference arrays along the horizontal and vertical directions of the JPEG's data array, then applying a Markov matrix to these difference arrays intra-block features are able to be constructed.{{Cite journal |last=Zhou |first=Zhiping |last2=Hui |first2=Maomao |date=August 2009 |title=Steganalysis for Markov Feature of Difference Array in DCT Domain |url=https://ieeexplore.ieee.org/document/5360076 |journal=2009 Sixth International Conference on Fuzzy Systems and Knowledge Discovery |volume=7 |pages=581–584 |doi=10.1109/FSKD.2009.230}}
References
{{reflist}}