I have an array with 32 numbers. Initially, every number is 0, although it’s probably not important.
At any time I can change one number in this array.
I want to quickly find the minimum value and its index after such an update. Is there a way to do it in O(1) time?
almost everything you do on an array of size 32 is
O(1). Linear scan requires 32 comparisons, which is inO(1)O(1) = constant number of ops. If the array is of size 32 (or any fixed size for the matter), the number of ops is indeed constant (Think of it this way: you can replace the linear scan with a chained if conditions instead of a loop:
if (arr[0] < min), if (arr[1] < min) , ... if (arr[31] < min)For the thrill of it, regarding the general case for an array of size
n, it is not possible with compare based algorithms.If it was, we could sort in
O(n)using comparisons based algorithm:Assuming each op in the above algorithm is
O(1), and the loop iteratesntimes, your algorithm sorts A inO(n)– which cannot be done, since comparations based sorting are proved to beOmega(nlogn)problem