Jump to attached files
Special Elements
Giordano Colli
+TAGS: Prefix Sum, Two Pointers, Oracles, Binary Search, Sorting
+Difficulty: 1500
+Description:
+Problem Link: Codeforces Link
Problem Definition
Given an arrayMore formally
Count the number of special elements.
Simple Idea
The main idea is to check ifUsing this meta-algorithm, the problem reduces to checking if a subarray delimited by
Simple Checker
To check if a subarray delimited by- This algorithm scans all the possible distinct
pairs of
, or equivalently all the subarrays.These are
.
- For each of these subarrays, to calculate Sum
operations are performed.
Note that this time complexity is tight because there are at least
Just to calculate these is needed
The first algorithm that solves this problem requires
Time:
Is it possible to obtain a better time complexity ?
Precalculation Checker
The bottleneck of the previous checker is that each subarray sum is calculated for eachTo avoid these repeated calculations it is possible to precalculate all the sums one time and store them into an array.
The total time complexity is the same of the SIMPLE-CHECKER Algorithm, with the same Analysis.
There are
It is possible to use the oracle
The PRECALCULATION() takes
Checking if
Time:
Memory:
Is it possible to obtain a better time complexity ?
Binary Search Precalculation Checker
A bottleneck of the previous checker is that to check ifSince there can be
It is possible to sort elements of
Since the sort instruction takes
The PRECALCULATION-SORTED() takes
Checking if
Even if the checking now requires
This means that to speed up the algorithm it must speed up the precalculation time.
Time:
Memory:
Is it possible to obtain a better time complexity ?
Prefix Sum Binary Search Precalculation Checker
A bottleneck of the previous checker is the precalculation time.The only instruction that can be faster is Sum
It is possible to use prefix sums to speed up this process.
First, calculate the prefix sum array
The sum of a subarray is simple to represent using
It is easy to calculate the prefix array in
Since all the operations into the for loop are constant, the time required by the for loop is .
Sorting still requires time.
Since PRECALCULATION-ALGORITHM-SORTED-PREFIXSUMS() now requires
time and the for loop requires
time, the entire algorithm takes
time. The memory used is still
because the array
takes
memory.
Time:
Memory:
Is it possible to obtain a better time complexity ?
Binary Search Prefix Sums Checker
The main bottleneck of the previous algorithm is the precomputation.Observe that it is possible to represent each subarray sum as a difference of two elements of the prefix array
Notice that if all the elements
- fix
- for each
fix all the
- search the element
This algorithm takes
Nothing changed from the previous Algorithm with respect to time complexity.
The memory used to store additional data structures is
Time:
Memory:
Is it possible to obtain a better time complexity ?
Two Pointers Prefix Sums Checker
To reduce the time complexity of the previous algorithm it is needed to build a checker that better exploits the monotonicity of the prefix arrayWhere binary is useful, is often useful to use the two pointers approach.
It is possible to use the Two Pointers approach if there are 2 criteria:
- increase criterion: defines when a pointer must increase with respect to the considered subarray.
- decrease criterion: defines when a pointer must increase with respect to the considered subarray.
A proof is needed to ensure that a feasible subarray is considered by the two pointers.
Place
Consider the following criteria(
- increase criterion: if the subarray sum is
increase the value of
.
- decrease criterion: if the subarray sum is
increase the value of
.
Proposition 8.1 Â Given the criteria
, each subarray
that sums to
is considered.
Proof. Consider each feasible subarray, i.e.
. Since
there are two possible cases.
has been already analyzed.
Case 1:
and
Case 2:
and
From the previous proposition derives an efficient checker that runs in
time.
The presented algorithm runs in
time.
memory to store the prefix sum array
.
time, in other words, the problem must have a lower bound of
time.
and
and
Case 1:
Due to the monotonicity of ,
. This implies that the pointed subarray has a sum lower than
:
Due to the monotonicity of ,
. This implies that the pointed subarray has a sum greater than
:
The presented algorithm runs in
time is used to precalculate the prefix sum array.
time is used to check if there is a subarray that sums to
for each
Improvements
The author conjectures that it is not possible to design a non-randomized algorithm that solves this problem in