Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

O(N+M) time complexity

Tags:

I'm working through some practice problems where I'm given a target time complexity and space complexity. One of them gives a target time complexity of O(N+M). I'm having some trouble with the intuition of what an O(N+M) algorithm would look like. Does anyone have an example of an algorithm like this or can explain it clearly? Every example I try to think of seems like O(N*M) to me.

like image 410
user137717 Avatar asked Sep 11 '14 20:09

user137717


People also ask

Is O nm linear time?

To sum up: O(mn) is generally called linear for things like matrix multiplication because it's linear in the size of the input, but it's generally called quadratic for things like string matching because of the smaller input.

Is O log N or O n faster?

For the input of size n , an algorithm of O(n) will perform steps perportional to n , while another algorithm of O(log(n)) will perform steps roughly log(n) . Clearly log(n) is smaller than n hence algorithm of complexity O(log(n)) is better. Since it will be much faster.

What is O n time complexity called?

The algorithm will just iterate through the array once, which results a time complexity of O(n). Therefore, we would say that the best-case time complexity of insertion sort is O(n). A complexity of O(n) is also often called linear complexity.

What is Big O of n log n?

O(nlogn) is known as loglinear complexity. O(nlogn) implies that logn operations will occur n times. O(nlogn) time is common in recursive sorting algorithms, sorting algorithms using a binary tree sort and most other types of sorts. The above quicksort algorithm runs in O(nlogn) time despite using O(logn) space.


1 Answers

A simple example of algorithm that is O(m+n):

int sum(int[] nArr, int[] mArr) {     int sum = 0;     for(int i : nArr) {         sum += i;     }     for(int i : mArr) {         sum += i;     }     return sum; } 

To compute the sum, you need to go through all elements in nArr (size n) and all elements in mArr (size m), so the overall complexity is O(m+n)

like image 113
Jean Logeart Avatar answered Oct 11 '22 19:10

Jean Logeart