Questions tagged [big-o]

The Big-O notation is used to represent asymptotic upper bounds. It describes relevant time or space complexity of algorithms. Big-O analysis provides a coarse and simplified estimate of a problem difficulty.

The Big-O notation is used to represent asymptotic upper-bounds. It allows a person to see if a problem will take years or seconds to compute on a modern computer.

In computer science, it is most commonly used when talking about the time complexity of algorithms, but can also refer to the storage required.

For example, a linear search on an unsorted array of size N, is O(N). If we put the elements first in a hash table, the space used is O(N) (Theta(N) to be more precise), but the search time is O(1) in the average case.

It should be noted that Big-O only represents an upper bound for a function. Therefore an O(N) function will also be O(NlogN), O(N²), O(N!), etc. In many cases Big-O is used imprecisely and Big-Theta should be used instead.

If a complexity is given by a recurrence relation, an analysis can often be carried out via the Master Theorem.

Properties

  • Summation
    O(f(n)) + O(g(n)) -> O(max(f(n), g(n)))
    For example: O(n^2) + O(n) = O(n^2)

  • Multiplication by a positive constant
    O(c * f(n)) -> O(f(n))
    For example: O(1000 * n^2) = O(n^2)

  • Multiplication
    O(f(n)) * O(g(n)) -> O(f(n) * g(n))
    For example: O(n^2) * O(n) = O(n^2 * n) = O(n^3)

  • Transitivity
    f(n) = O(g(n)) and g(n) = O(h(n)) then f(n) = O(h(n))

Groups of Big-O

Complexity Sample algorithms
O(N!) Get all permutations of N items
O(2^N) Iterating over all subsets of N items
O(N^3) Calculating all triplets from N items
O(N^2) Enumerating all pairs from N items, insert sort
O(NLog(N)) Quick sort, merge sort
O(N) Getting min, max, average, iterating over N items
O(Log(N)) Binary search
O(1) Getting an item by the index in the array

More info

6779 questions
2
votes
1 answer

Big O: What is the name for the complexity O(a * b)?

I am new to studying the Big O notation and have thought of this question. What is the name for the complexity O(a * b)? Is it linear complexity? polynomial? or something else. The code for the implementation is below. function twoInputsMult(a, b)…
Nugget
  • 81
  • 1
  • 6
  • 23
2
votes
0 answers

Optimizing time complexity of prefix searching between two lists

Looking to refactor some legacy code, and I have a method that's functionally similar to the following simplified version: public static List getAllPrefixedCodes(List codes, List prefixes) { var prefixedCodes = new…
RMSD
  • 476
  • 5
  • 12
2
votes
1 answer

Why is looking up an array value at a given index O(n) time complexity

I have read that to look up the value at index n of an array, the computer will calculate (array start location) + (n * (data type size)) to find the location in memory of the value. Would this not take more than O(1) time because multiplication…
2
votes
0 answers

Big-O for traversing perimeter of 2D array

Suppose I have a m x n array and want to perform an operation on just its perimeter as follows: for(let i = 0; i < m; i++){ for(let j = 0; j < n; j++){ if(i * j === 0 || i === m - 1 || j === n - 1){ // some function that has O(m * n)…
lbragile
  • 7,549
  • 3
  • 27
  • 64
2
votes
2 answers

TapeEquilibrium - Python

I'm studying through Codility and I'm doing this lesson. So the basically solution that I thought at first time was: #SOLUTION 1 def solution(A): diff = [] for x in range(1,len(A)): first_half = sum(A[:x]) second_half =…
2
votes
1 answer

Search for a value in a column wise sorted array in linear time

How can I search for a value in a column-wise sorted array in linear time O(n)? I need to write a function that takes in a column-wise sorted 2d array, and returns true if a specified value is in the array. Example of a column-wise sorted…
Serket
  • 3,785
  • 3
  • 14
  • 45
2
votes
1 answer

Time Complexity of Hash Map Traversal

What is the best, average and worst case time complexity for traversing a hash map under the assumption that the hash map uses chaining with linked lists. I've read multiple times that the time complexity is O(m+n) for traversal for all three cases…
Frederik
  • 1,221
  • 2
  • 13
  • 22
2
votes
2 answers

How does 20 * 2 billion take as long as 2 * 3 in Big O Notation?

Im studying big O and I came across this function timesTwo(num) { return 2 * num } let result = timesTwo(5) // 10 let result2 = timesTwo(2000) // 4000 then it says this Now, which of these do you think will take the longest to compute? 2 * 5 or…
2
votes
1 answer

Big O notation for these algorithm complexities

I have a few algorithm complexities that I'm not entirely sure of what the Big O notations are for them. i) ((n-1)(n-1) * ... * 2 * 1)/2 ii) 56 + 2n + n^2 + 3n^3 iii) 2n(lg n) + 1001 iv) n^2 * n^3 + 2^n I believe ii) and iii) are pretty…
Maximus
  • 91
  • 5
2
votes
2 answers

What is the time complexity of this algorithm if I know n > m?

I have the following algorithm that finds the items in common of two sorted lists: findIntersection(List1, List2) { list intersection; int i = 0; int j = 0; while i < size of List1 and j < size of List2 { if List1[i] <…
2
votes
1 answer

Why is there not an implementation of a linked list in JavaScript?

For example Java has both ArrayList and LinkedList which behave as one would expect regarding Big O. JavaScript has array [], which behaves like a dynamic array as you can insert and delete to it wherever you prefer, in the middle at the end…
user18924622
2
votes
2 answers

Time complexity of nested loop?

I'm not very familiar with Big O notation. I'm wondering does the time complexity of the nested loop always be n^2? For example the following function, the inner loop runs 100 times per loop, does it be considered as n times as well? What's the time…
dododoo
  • 21
  • 1
2
votes
2 answers

If stack operations are constant time O(1), what is the time complexity of this algorithm?

BinaryConversion: We are inputting a positive integer n with the output being a binary representation of n on a stack. What would the time complexity here be? I'm thinking it's O(n) as the while loop halves every time, meaning the iterations for a…
2
votes
1 answer

Big O notation of "addition"

I'm learning a course about big O notation on Coursera. I watched a video about the big O of a Fibonacci algorithm (non-recursion method), which is like this: Operation Runtime create an array F[0..n] O(n) F[0] <-- 0 …
Hoàng
  • 73
  • 6
2
votes
1 answer

What is the time complexity of looping over the array and then splitting the number into digits?

If we had a case where we are looping over the array of numbers and the numbers can scale to infinity, and inside of each iteration we are looping over digits of each number, so for number 125556 we would loop through six numbers, 1, 2, 5, 5, 5, and…
SrdjaNo1
  • 755
  • 3
  • 8
  • 18