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
4 answers

Data structure that supports the following in O(1) time: initialization, insertion, deletion, finding an element, deleting all elements

Interview Question: Propose a data structure that holds elements from 0 to n − 1 and supports all of the following operations in O(1) time: initialization, insertion of an element, deletion of an element, finding an element, deleting all…
user7
  • 2,339
  • 5
  • 25
  • 29
2
votes
2 answers

How do I solve this problem (leetcode-style technical problem)?

Say you are given a list of integer pairs pairs and two integers k1 and k2. Find the count of pairs of pairs from the list that fulfills: pairs[i][0] + pairs[j][0] <= k1 pairs[i][1] + pairs[j][1] <= k2 for i < j For example, if,…
seekerpig
  • 47
  • 2
2
votes
1 answer

How complexity differs from calling a function actually n times or putting inside a loop that iterates n times?

Let's say the function simply prints the elements of an array. I know the time complexity of the following loop version will be O(n). static void printArray(int[] myArray) { for(int i = 0; i < myArray.length; i++) { …
2
votes
1 answer

what is the time complexity of an algorithm that recursively checks if a string is a palindrome?

I have solved the problem using this algorithm and have calculated that the total number of recursive calls is n/2 since we always pass a substring of the original string with 2 less characters. The runtime complexity of the slice() function is…
WilliamG
  • 31
  • 5
2
votes
1 answer

Outer loop linear, inner loop logarithmic, complexity analysis

What is the complexity for the following piece of code, O(logn) or O(nlogn)? int i=1; while (i<= n) { int j = i; while (j > 0) { j = j/2; } i++; } I asked chatgpt and sometimes it says O(logn) and sometimes O(nlogn). The…
Silidrone
  • 1,471
  • 4
  • 20
  • 35
2
votes
1 answer

What is the time complexity of function2?

int function2(int n) { int i, j, k; int sum = 0; for (k = 1; k <= n; k += 1) { for (i = 1; i <= n; i *= 3) { j = i; while (j > 1) { sum += 1; j /= 3;…
Zhou Fang
  • 23
  • 3
2
votes
3 answers

Time complexity of recursion of multiplication

What is the worst case time complexity (Big O notation) of the following function for positive integers? def rec_mul(a:int, b:int) -> int: if b == 1: return a if a == 1: return b …
2
votes
1 answer

Big-O of a function that has a nested loop affected by outer loop

for(int i = 0; i < N*N; i++) { for(int j = 0; j < i; j++) { //something O(1) } } So I was trying to find big O of this function. The outer loop is O(N^2) alone, and the inner loop goes from 0 to N^2. So I was thinking this would make…
Adam M
  • 21
  • 1
2
votes
2 answers

Big Omega Notation. Am I doing this right?

If I have some algorithm that runs at best n time and at worst n^2 time, is it fair to say that the algorithm is Big Omega (n)? Does this mean that the algorithm will run at least n (time)? I am just not sure if I have the right idea here. Thanks.
Spencer
  • 95
  • 1
  • 1
  • 5
2
votes
4 answers

Finding a particular Set of SubSets of a PowerSet in an efficient way

I'm trying to find an efficient way to grab a Set of Subsets of a PowerSet. For example, this works when the set sizes are small: Set set = new HashSet(); set.add(1); set.add(2); set.add(3); Set set2 = new…
Shawn
  • 7,235
  • 6
  • 33
  • 45
2
votes
2 answers

What would the big O notation be for a 3x3 multidimensional array in java?

I am having trouble figuring out how to calculate the big O notation for a project. What would the big O notation be for a 3x3 grid, or a nxn grid with n being decided by the user. Also, if I output a 3x3 grid multiple times, do I add the big O…
nono
  • 21
  • 1
2
votes
1 answer

What is the time complexity (Big-O) of this while loop (Pseudocode)?

This is written in pseudocode. We have an Array A of length n(n>=2) int i = 1; while (i < n) { if (A[i] == 0) { terminates the while-loop; } doubles i } I am new to this whole subject and coding, so I am having a hard time…
aurora
  • 23
  • 4
2
votes
0 answers

Is my assumption about Time and Space Complexity of subsum algorithm correct?

Is the space complexity of this algorithm is O(n^3) cubic space complexity ? As: list is an array, subs is an array + one array created with map. Is the time complexity is O(n^4) biquadratic? As: two times loop on list + one time map + one time…
Daria
  • 29
  • 1
2
votes
3 answers

Count character occurrences in a substring in O(1) time with preprocessing

This question has not been asked before, since I am specifically asking O(1) constant time after the time taken to preprocess the string is amortized over many freq operations. An interviewer asked me to find the total count of a character appearing…
R0bin_0
  • 31
  • 5
2
votes
2 answers

Calculating the Number of Pairs of elements in a List that produce a Sum divisible by 60 - reduce the Time Complexity

There are many permutation optimization questions, but every one is different. At a coding assignment recently, I was asked, given a list of numbers, to find how many pairs add up to a multiple of 60. I've come up with was the following…
f.khantsis
  • 3,256
  • 5
  • 50
  • 67