Provide an example of O(1) algorithm

Technology CommunityCategory: Big-O NotationProvide an example of O(1) algorithm
VietMX Staff asked 3 years ago

Say we have an array of n elements:

int array[n];

If we wanted to access the first (or any) element of the array this would be O(1) since it doesn’t matter how big the array is, it always takes the same constant time to get the first item:

x = array[0];