Reputation: 145
I have a scenario where - has to read two iterators and need to add them to list and return that list here what is "The function should operate in O(1) time".
If my understanding is correct, if array has 1 element and the process time should take 1 sec and if it has 100 elements then also it should take 1 sec...
how can i achieve the 1 sec algorithm here for above read and add to new list operation...
Upvotes: 0
Views: 3099
Reputation: 12272
O(1)
time complexity means that whatever is the size of the input, the time taken by the program to run is constant.
That is the reason O(1)
time complexity is called constant time complexity.
You can compare this with other time complexities, for example O(n)
this means that the time taken for the program increases linearly with the size of the input, i.e. n
.
O(exp^n)
means the time taken will increase exponentially.
Upvotes: 5
Reputation: 26077
O(1)
O(1) describes an algorithm that will always execute in the same time (or space) regardless of the size of the input data set. O(1) time complexity is also called constant time complexity
Independent of List Size, always return first element.
boolean IsFirstElementNull(List<string> elements)
{
return elements[0] == null;
}
O(N)
O(N) describes an algorithm whose performance will grow linearly and in direct proportion to the size of the input data set.
boolean ContainsValue(List<string> elements, string value)
{
foreach (var element in elements)
{
if (element == value) return true;
}
return false;
}
Upvotes: 8