Questions tagged [time-complexity]

The time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the size of the input to the problem. The time complexity of an algorithm is commonly expressed using big O notation, which suppresses multiplicative constants and lower order terms.

In computer science, the time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the size of the input to the problem. The time complexity of an algorithm is commonly expressed using big O notation, which suppresses multiplicative constants and lower order terms.

When expressed this way, the time complexity is said to be described asymptotically, i.e., as the input size goes to infinity. For example, if the time required by an algorithm on all inputs of size n is at most 5n^3 + 3n, the asymptotic time complexity is O(n^3).

Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, where an elementary operation takes a fixed amount of time to perform. Thus the amount of time taken and the number of elementary operations performed by the algorithm differ by at most a constant factor.

Since an algorithm may take a different amount of time even on inputs of the same size, the most commonly used measure of time complexity, the worst-case time complexity of an algorithm, denoted as T(n), is the maximum amount of time taken on any input of size n. Time complexities are classified by the nature of the function T(n).

For instance, an algorithm with T(n) = O(n) is called a linear time algorithm, and an algorithm with T(n) = O(2^n) is said to be an exponential time algorithm.

Useful links:

Related tags:

10064 questions
41
votes
4 answers

What is the time complexity of heapq.nlargest?

I was looking at this pycon talk, 34:30 and the speaker says that getting the t largest elements of a list of n elements can be done in O(t + n). How is that possible? My understanding is that creating the heap will be O(n), but what's the…
foo
  • 940
  • 2
  • 9
  • 20
41
votes
2 answers

What's the time complexity of iterating through a std::set/std::map?

What's the time complexity of iterating through a std::set/std::multiset/std::map/std::multimap? I believe that it is linear in the size of the set/map, but not so sure. Is it specified in the language standard?
updogliu
  • 6,066
  • 7
  • 37
  • 50
40
votes
6 answers

What is the complexity of these Dictionary methods?

Can anyone explain what is the complexity of the following Dictionary methods? ContainsKey(key) Add(key,value); I'm trying to figure out the complexity of a method I wrote: public void DistinctWords(String s) { Dictionary d =…
Dan Dinu
  • 32,492
  • 24
  • 78
  • 114
40
votes
5 answers

Time complexity of System.arraycopy(...)?

System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) is a native method. What is the time complexity for this method?
Kowser
  • 8,123
  • 7
  • 40
  • 63
40
votes
1 answer

Time complexity of string concatenation in Python

I'm analysing the complexity of my code. From what I found online, since strings are immutable in python, a concatenation of a string and a character should be O(len(string) + 1). Now, here is my piece of code (simplified): word = "" for i in…
cwbrd
  • 547
  • 1
  • 4
  • 10
40
votes
8 answers

Triplet whose sum in range (1,2)

Given n positive real numbers in an array, find whether there exists a triplet among this set such that, the sum of the triplet is in the range (1, 2). Do it in linear time and constant space. the array is not ordered. numbers are…
Trying
  • 14,004
  • 9
  • 70
  • 110
39
votes
4 answers

What is the cost/ complexity of insert in list at some location?

In Python, a list has list.insert(i, x) to "Insert an item at a given position.". In C++, there is a list as well. In C++, cost/complexity of inserting an element anywhere is O(1). Is it the same for a Python list? If not, can anything else be use…
user3654650
  • 5,283
  • 10
  • 27
  • 28
38
votes
5 answers

matrix multiplication algorithm time complexity

I came up with this algorithm for matrix multiplication. I read somewhere that matrix multiplication has a time complexity of o(n^2). But I think my this algorithm will give o(n^3). I don't know how to calculate time complexity of nested loops. So…
zedai
  • 523
  • 2
  • 6
  • 11
38
votes
4 answers

Why does heap sort have a space complexity of O(1)?

I understand that both quick sort and merge sort need O(n) auxiliary space for the temporary sub-arrays that are constructed, and in-place quick sort requires O(log n) auxiliary space for the recursive stack frames. But for heap sort, it seems like…
Herman Tran
  • 1,581
  • 2
  • 12
  • 19
37
votes
7 answers

Why does this O(n^2) code execute faster than O(n)?

I have written code for two approaches to find out the first unique character in a string on LeetCode. Problem Statement: Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Sample…
user63762453
  • 1,734
  • 2
  • 22
  • 44
37
votes
9 answers

Pop multiple values from Redis data structure atomically?

Is there a Redis data structure, which would allow atomic operation of popping (get+remove) multiple elements, which it contains? There are well known SPOP or RPOP, but they always return a single value. Therefore, when I need first N values from…
Pavel S.
  • 11,892
  • 18
  • 75
  • 113
37
votes
3 answers

String concatenation complexity in C++ and Java

Consider this piece of code: public String joinWords(String[] words) { String sentence = ""; for(String w : words) { sentence = sentence + w; } return sentence; } On each concatenation a new copy of the string is created, so…
ethanjyx
  • 1,970
  • 7
  • 28
  • 50
36
votes
4 answers

Binary search vs binary search tree

What is the benefit of a binary search tree over a sorted array with binary search? Just with mathematical analysis I do not see a difference, so I assume there must be a difference in the low-level implementation overhead. Analysis of average…
john
  • 721
  • 3
  • 9
  • 13
35
votes
44 answers

Given a list of numbers and a number k, return whether any two numbers from the list add up to k

This question was asked in the Google programming interview. I thought of two approaches for the same: Find all the subsequences of length. While doing so compute the sum and of the two elements and check if it is equal to k. If ye, print Yes, else…
35
votes
1 answer

Add to SortedSet and its complexity

MSDN states the following SortedSet(T).Add Method : If Count is less than the capacity of the internal array, this method is an O(1) operation. Could someone please explain "how so"? I mean when adding new value we need to find a correct place to…
Andrey Taptunov
  • 9,367
  • 5
  • 31
  • 44