I can’t give you a rigid mathematical test, but I say with certainty that what is asked is impossible. For any case not trivial:
k < 0
(only admits the empty vector with input, the return is zero)
a > k
(the return is always zero)
b < 0
(the return is always zero)
b < a
(the return is always zero)
a = 0 E b = k
(the return is always n
- assuming that the size of the vector can be determined in constant time)
it is possible to produce two distinct vectors:
V + [x]
, where x ∈ [a,b]
V + [y]
, where y ∉ [a,b]
which will produce different results if evaluated by this function.
Suppose there is an algorithm with complexity O(1)
. So, for vectors of size greater than or equal to n0
there is a constant K
such that the running time is less than or equal to K
. Be it V
the vector (one of the vectors? ) whose runtime is the longest of all (i.e. K
), and whose return value is R
. How this algorithm would evaluate V + [_]
?
- If it does not perform any operation additional to those it has already performed to evaluate
V
, then he can’t say deterministically if the result is R
or R + 1
- since the additional element may be at least x
or y
;
- If it performs one or more additional operations, then its run time is greater than
K
(assuming an operation has run time greater than zero). That is, the algorithm has no order of complexity O(1)
, as an assumption.
By the way, this also means that - contrary to the assumption - the vector V
was not in fact the one who possesses the "longest execution time of all". In fact, such a vector does not exist - since the set of all possible vectors with elements of 0
to k
is infinite (even if enumerable). Any vector you choose as "the largest" can - as demonstrated - give rise to a vector whose runtime is at least one elementary operation greater than it.
The best algorithm that solves this problem will have complexity of O(n)
. It is inevitable that all elements of the vector are visited at least once - either to compare, or to include in a count, or to be the object of an arithmetic operation. Unless there is some "catch" I’m not seeing, this exercise has no solution...
I haven’t started anything yet because I haven’t thought of any algorithm. The fastest I know (linear ordering) do not meet because they are of complexity O(n). It would be possible to solve this problem with complexity O(1)?
– Gustavo Nunes
Do you want someone to do your exercise? Besides, you didn’t explain anything, you didn’t show what you already did, you didn’t talk about language. Try to do something, then explain better what your doubt, what language, paste the code and we can help.
– Paulo
Gustavo, welcome to the site. I edited your question to solve two problems, which led to votes against and to close: (1) the questions here need to be self-sufficient, and yours required a visit to an external link; (2) it looked like you were asking to solve your homework, and from your comments I understood that the problem is different, so I edited accordingly. If you disagree, you can change it again, or reverse my edit.
– bfavaretto
@Paulomaciel Eu think that the question wasn’t quite what it looked like, take a look at the issue I did.
– bfavaretto
@bfavaretto now became clearer, before he only asked to solve an exercise without making clear the problem
– Paulo
Thank you very much people! I am new here and did not know very well how it worked, thank you very much for the help and sorry anything! =)
– Gustavo Nunes
You are welcome! This site really works a little different from the others, I suggest doing the tour and take a look at help center.
– bfavaretto
I know an algorithm to perform the sum of n numbers of any vector in O(lg n) time, but for this it needs a previous preparation process to assemble a table, whose time is O(n). Supposing the statement is correct, wouldn’t that be the case that the teacher asks? It may be that the requested algorithm has a longer preparation time than O(1), but then, during operation, it has O(1)... Almost how a hash table works? I’m curious now!
– carlosrafaelgn