Sobes.tech
Junior — Middle

Kui kiiresti saab elemente kätte, kui pöördute selle poole selle positsiooni järgi massiivis?

sobes.tech AI

Vastus AI-lt

Accessing an array element by its position occurs in constant time — O(1). This is because arrays store elements in a contiguous memory region, and the address of the required element is calculated using the formula: base address + index * size of element. For example, in Kotlin or Java, you can get an element like this:

val array = arrayOf(10, 20, 30, 40)
val element = array[2] // Fast access to the element with value 30

Such access is very efficient and does not depend on the size of the array.