What is the time complexity of accessing an element in an array?
a) O(log n)
b) O(n)
c) O(1)
d) O(n log n)
Answer:
c) O(1)
Explanation:
Accessing an element in an array is done in constant time O(1), as arrays provide direct access to any element using its index. This makes arrays a very efficient data structure for accessing elements.
The index is used to calculate the memory address of the element, and this operation takes the same amount of time regardless of the size of the array.
However, operations like searching, inserting, or deleting in unsorted arrays can take linear time O(n), depending on the position of the element.
Reference:
DSA (Data Structures and Algorithms) MCQ Questions and Answers