-2

Can anybody tell me what is the time complexity of this:

vector<int>arr; vector<int>v(arr.begin(),arr.end());

Is is it O(N) or O(1) ?

spectre009
  • 124
  • 1
  • 1
  • 10
  • Do you want the complexity of this code in isolation, or is there some process filling `arr` with data? –  Jun 30 '18 at 18:31
  • O(N), discussed for instance [here](https://stackoverflow.com/questions/421573/best-way-to-extract-a-subvector-from-a-vector). However, In the example you show, the time will be 0, as `arr` is empty. – JHBonarius Jun 30 '18 at 18:32
  • 2
    Ignoring the question of how many items are in `arr`... how could it be O(1)? – Nicol Bolas Jun 30 '18 at 18:35
  • Think about it. You're copying every element of `arr`. – eesiraed Jun 30 '18 at 23:47

1 Answers1

0

It is O(N). v's constructor has to construct the elements from arr having setup the memory for it. N is arr.size()

SJHowe
  • 756
  • 5
  • 11