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

Logarithmic complexity: Either the book has a typo or what's happening here?

I am going through algorithms now and I've faced one example where I answered as an Infinite loop but in the correct answers, it says it's O(log2n). function someFunc(n) { for(var i = 0; i < n; i * 2) { // I think that Infinite loop cannot be…
user16373392
2
votes
1 answer

The Game of Piles

Recently I have encountered what seems to be a quite interesting game that suggest implementing both two-pointers and prefix-sum techniques on a large dataset. Here is the task itself: Imagine there is an array v of length k where k (k<=10**5)…
Ramiil
  • 59
  • 8
2
votes
1 answer

Javascript Array: what is a Big O of performing sort and then map right after on it?

arr.sort((a, b) => a - b).map(num => num ** 2); What would be a Big O of the following operation? As I understand Big O of imbedded sort function in JS is O(Nlog(N)) and Big O of map is O(N), therefore Big O is O(Nlog(N))?
Konstantink1
  • 575
  • 1
  • 8
  • 26
2
votes
2 answers

If f(n) = θ(g(n)), and for every , (),()≥ 2 then log(f(n)) = θ(log(g(n)))?

Is this statement false? I couldn't find a answer to this question in this site (only for big O). from the definition i can assume: log⁡(C1g(n)) ≤ log⁡(f(n)) ≤ log⁡(C2g(n)) i solved the right side: log⁡(f(n)) ≤ log⁡(C2g(n)) ≤ log⁡(C2) + log⁡(g(n)),…
Danny
  • 21
  • 2
2
votes
1 answer

Analyze the time cost of the following algorithms using Θ notation

So many loops, I stuck at counting how many times the last loop runs. I also don't know how to simplify summations to get big Theta. Please somebody help me out! int fun(int n) { int sum = 0 for (int i = n; i > 0; i--) { …
Liafonx
  • 31
  • 4
2
votes
1 answer

Time complexity of nested for loops where inner loop depends on outer loop

I have a nested for loop: for(i = 0; i < n*n; i++) for(j = 0; j <= i/5; j++) print("Hello World!"); How do I find the time complexity of this loop. I was thinking for the outer loop, it runs from 0 to n^2 (exclusive), so it runs n^2 + 1…
bigoh392e
  • 23
  • 3
2
votes
1 answer

Big O Notation: For loop with O(1) operation on the inside

I'm working on understanding big O notation a little bit better and got stumped on this problem in class. If I had a for loop with a constant number of iterations that simply prints something to the console on each iteration: for(int i=1; i<10;…
banna
  • 165
  • 11
2
votes
5 answers

MaxProductOfThree how to increase the performance

MaxProductOfThree lesson : https://app.codility.com/programmers/lessons/6-sorting/max_product_of_three/ For solving this Codility lesson I've coded this function like below; import itertools def solution(A): combs=[] for pivot_element in…
Berke Şentürk
  • 119
  • 1
  • 12
2
votes
2 answers

Time complexity analysis of two algorithms contradicts empirical results

I wrote the following simple function that checks whether str1 is a permutation of str2: def is_perm(str1, str2): return True if sorted(str1)==sorted(str2) else False Assuming that sorted(str) has a time complexity of O(n*logn), we can…
Moritz Wolff
  • 436
  • 1
  • 7
  • 16
2
votes
1 answer

Big O notation g(n) ∈ O(f(n)) =⇒ (g(n))^2 ∈ O((f(n))^2)

My question is, is this True. g(n) ∈ O(f(n)) =⇒ (g(n))^2 ∈ O((f(n))^2) At the long run it should be true, but i have one example (log n )^ 2 which is still in or less then O(sqrt n) Is there a way to proof this without the graph. THX
logn
  • 113
  • 4
2
votes
3 answers

Is O(mn) better than O((m+n)^2)?

The inputs to the algorithm are m and n. The time complexity of my algorithm comes out to be O(mn). I have a benchmark algorithm that has a time complexity of O((m+n)²). Is my implementation better than the benchmark in terms of time complexity?
2
votes
1 answer

Why is O(log(n)) coming equal to O(log(n!))?

While solving the complexity of a code, I found it as O(log(n!)). I know that this can be proven equal to O(n*log(n)). However, can someone tell where this proof is going wrong? Theorems used: log(ab) = log(a) + log(b) O(a+b) =…
2
votes
3 answers

Shouldn't the time complexity of binary search be O(ceil(logn))?

This seems to be a common question and the answer can be found anywhere, but it's not the case: I cannot find an answer anywhere on the Internet. The point is, I've never seen anywhere asking a question about whether the time complexity might be…
Searene
  • 25,920
  • 39
  • 129
  • 186
2
votes
0 answers

Time complexity nuances in JavaScript

What is the time complexity of the algorithm below ? I suppose it's O(n), because of traversing through the array and O(n), because of reversing array. All in all it's O(2n) -> O(n). Am i right ? I tried to avoid the one-line brute force approach,…
2
votes
1 answer

A good way to prevent string repeats in java without O(n) operations in Java?

Here's a hypothetical situation: you want to automatically assign unique nicknames to unique names, with no repeats. Since nicknames contain less text information than the original name, as that is the nature of nicknames, it's not guaranteed that…
Wasabi Thumbs
  • 303
  • 1
  • 2
  • 8
1 2 3
99
100