Draft:Difference array: Difference between revisions

Content deleted Content added
Heyyo53 (talk | contribs)
Range Queries: Added a proof, maybe not the most substantial one but I will edit later
Declining submission: nn - Submission is about a topic not yet shown to meet general notability guidelines (be more specific if possible) (AFCH)
Line 1:
{{AFC submission|d|nn|u=Heyyo53|ns=118|decliner=ToadetteEdit|declinets=20250521084736|ts=20250520231757}} <!-- Do not remove this line! -->
 
{{Short description|Computer Science Algorithm}}
{{Draft topics|stem}}
{{AfC topic|stem}}
 
{{AfC submission|||ts=20250520231757|u=Heyyo53|ns=118}}
{{AfC submission|t||ts=20250520231634|u=Heyyo53|ns=118|demo=}}
<!-- Important, do not remove anything above this line before article has been created. -->
 
Line 22 ⟶ 23:
 
== 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>. 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" />