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

Determine the time complexity of an algorithm

I'm reading one cool book, and there was a task to determine the time complexity of the code in the worst case. The code performs integer division (a > 0, b > 0): int div(int a, int b) { int count = 0; int sum = b; while (sum <= a) { …
Serhii Chernikov
  • 353
  • 2
  • 11
2
votes
1 answer

Define the Time Complexity for the algorithm (Java)

I have the following snippet of code: public static int digPow(int n, int p) { int powCounter = p; int sum = 0; char[] digits = (""+n).toCharArray(); for (char digit : digits) { sum += Math.pow(Character.getNumericValue(digit),…
Serhii Chernikov
  • 353
  • 2
  • 11
2
votes
4 answers

How to calculate big-theta

Can some one provide me a real time example for how to calculate big theta. Is big theta some thing like average case, (min-max)/2? I mean (minimum time - big O)/2 Please correct me if I am wrong, thanks
Navin Leon
  • 1,136
  • 3
  • 22
  • 44
2
votes
0 answers

Why this for loop is faster than the commented for loop?

I'm solving this leetcode problem (problem no:55): You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.** Return true if…
2
votes
2 answers

Why is it O(log(logn)) instead of O(logn)

p = 0 for( i=1; i
mark
  • 81
  • 7
2
votes
2 answers

Big-O notation for complex loop

I can not guess the Big-O notation for this loops. for (int j = 2; j < N; j++) { for (int k = 2*j; k <= N; k += j) { nums[k] = false; } } How to calculate the algorithm complexity for this loop?
David Zhang
  • 23
  • 1
  • 6
2
votes
1 answer

Find the 2^k largest elements in O(n)

Given an array A, the task is to find the largest, 2nd largest, 4th largest, 8th largest, 16th largest, ... and so on up to 2^logn largest element. Give an O(n) algorithm to do this. I do understand that elements need not to be sorted between the…
gonidelis
  • 885
  • 10
  • 32
2
votes
2 answers

What counts as an operation in algorithms?

So I've just started learning algorithms and data structures, and I've read about Big O and how it portrays complexity of algorithms based on how the number of operations required scales But what actually counts as an operation? In this bubble sort,…
Akzhol
  • 83
  • 1
  • 6
2
votes
2 answers

Calculating time complexity of Python while loop

I have been working on DSA problems and have also been trying to calculate the time and space complexity of my solutions. I feel like I can do ok with most solutions, however there are many, like the one below, that stump me. while paymentTotal…
dSisk
  • 25
  • 6
2
votes
1 answer

Trouble with nested for-loop running time

I have been thinking over this problem for a few days now and am hung up on calculating the number of times the second nested for-loop will run. I believe that I have the correct formula for determining the running time for the other two for-loops,…
John
  • 81
  • 2
  • 5
2
votes
4 answers

Given an array of integers A and an integer m, determine the maximum product of m consecutive integers in the array

import java.lang.reflect.Array; import java.util.*; public class Part1 { public static int maxProduct(Array[] a, int m) { int maxProduct = Integer.MIN_VALUE; if (a.length < m) { return 0; } for (int i = 0; i…
Jerome
  • 39
  • 6
2
votes
1 answer

Is little O the complement of Theta to Big O

In other words, is o(f(n)) = O(f(n)) - Θ(f(n))? f ∈ O(g) [big O] says, essentially For at least one choice of a constant k > 0, you can find a constant y such that the inequality 0 <= f(x) <= k g(x) holds for all x > y. f ∈ Θ(g) [theta] says,…
2
votes
3 answers

When f(n) = n^.1 and g(n) = log(n)^10, does f(n) = Ω(g)?

I was told that "any exponential trumps any logarithm". But when the exponential is between zero and one, doesn't the execution time of the logarithm grow much faster? So by that logic it would be f = O(g) I'm having trouble choosing whether to…
Outback
  • 542
  • 2
  • 8
  • 20
2
votes
1 answer

Delete and Increase key for Binomial heap

I currently studying the binomial heap right now. I learned that following operations for the binomial heaps can be completed in Theta(log n) time.: Get-max Insert Extract Max Merge Increase-Key Delete But, the two operations Increase key and…
TONY
  • 25
  • 4
2
votes
3 answers

3SUM - O(n^2 * log n) slower than O(n^2)?

In the scenario I present to you, my solution is supposed to represent O(n^2 * log n), and the "pointers" solution, which I assume is the fastest way to resolve the "3SUM" problem, represents O(n^2 * 1); leaving the question of is O(1) faster than…
Bita
  • 139
  • 4