r/computerscience • • Oct 14 '26

Help Is the best case here O(1)?

Post image

Hi, still new to algorithm analysis and need some help confirming my answer or not.

I assumed that best case would be O(1) in this scenario if size of the array is either 0 or 1. But trying to review my answer with online resources, it say's that O(1) doesn't count here?

What is the definite answer here?

EDIT: Hey everyone! Glad everyone got to interact and give responses. The answer was O(N) in the end! Confirmed with my professor.

223 Upvotes

95 comments sorted by

4

u/Fit-Try9217 Oct 14 '26

hey everyone, thanks for the comments so far. I've decided to email my professor to get an elaboration on whether the edge cases (0 and 1) are allowed here.

if anyone would like to know, here are the two questions B and C that followed this question:

(b) How many times is line 12 executed in the best case? In the worst case? Express your answers as functions of n.

(c) Give the Big-O complexity of the function in the best case and in the worst case. Are they different? Explain why.

2

u/al2o3cr Oct 15 '26

Those followup questions strongly support the "part a means 'describe an input given a value of n'" hypothesis.

IMO you're missing the point of this exercise if you jump straight to O() notation.

Try counting operations exactly first, like this:

Part a:

Best case: an array of length n that has its maximum value at the beginning.
Worst case: an array of length n that is sorted from least to greatest

Justification:

Count primitive operations:

arr.length
compare n to zero
load arr[0]
store it in l
initialize i
loop n-1 times:
  compare i < n
  load arr[i]
  compare to l
  maybe:
    load arr[i]
    store it in l
return l

For the best-case scenario, this amounts to:

setup: 5
loop: (n-1) * 3
finish: 1

or a total of 6 + 3*(n-1) = 3*n + 3 operations

For the worst-case scenario:

setup: 5
loop: (n-1) * (3 + 2)
finish: 1

a total of 6 + 5*(n-1) = 5*n + 1 operations

Note: a smarter compiler will make some of these operations disappear (for instance, the second "load arr[I]" could be avoided with registers etc), but it's easier to analyze things like no optimizations are being done.

Part c of the question encourages you to notice that both have the same complexity in O-notation, despite having a different multiplier on n and a different constant term.

1

u/Kind_Card_1874 Oct 16 '26

I believe are missing the point. This question, based on OPs question, is about the asymptotic growth of the number of operations needed as a function of n. It is \Theta(n) in the best and the worst case. We always iterate proportional to n where n is the length of the input.

1

u/tim128 Oct 16 '26

Big O is just one way to express asymptotic growth and not very precise.

The algorithm will execute more operations if the input is in increasing order. The fact that Big O isn't precise enough to differentiate between them is irrelevant

2

u/Kind_Card_1874 Oct 16 '26

This is not ambiguous (in the RAM model). An array of length n will have time complexity \Theta(n), since it will iterate a number of times proportional to n up to constant factors. In the worst case, the behaviour is identical.

3

u/am_Snowie Oct 14 '26

b) not even once in the best case, in the worst case n - 1 times i guess (array is sorted). so it's just always gonna be theta(N) (you can even give a tighter bound here) since it's always gonna run N times.

c) since it's theta(N), we can just say it's O(N) as well.

feel free to point out if u find anything wrong.

1

u/SirClueless Oct 14 '26

Given that part (c) explicitly asks for Big-O complexity, and part (a) does not, you can assume that part (a) is asking for the exact best case and not something asymptotically equivalent.

Whatever answer you give should work for n=0 and n=1 and everything else. They are not "special cases", the question asks to describe "An input array of size n" -- if your answer doesn't work for n=1 and n=100000 it's not complete.

-3

u/YoungMaleficent9068 Oct 14 '26

N= 0 length is explicitly handled. In a horrible way. Never write code like that for real.

Noone on the entire planet is interested in line 12. That the job of a CPU with a branch predictor.

Worst and best case scenario are the same.

2

u/SnugglyCoderGuy Oct 14 '26 edited Oct 15 '26

Edit: I misread the second return as return <one> instead of return<el>. After this correction in understanding, please don't write functions like this and ignore everything below.

rst, this is obviously a toy function for educational purposes.

Second, returning 0 could absolutely be a valid thing to do when the array is empty depending on what this function is actually meant to do.

Third, we are interested in line twelve because again this is for educational purposes and the purpose here is to reason about the code.

Forth, Worst and best case scenario are not the same.

Please better yourself.

1

u/Marisakis Oct 15 '26

The function returns the largest element out of the array. That's what it's 'meant to do'. If it's not meant to do that, it should have a descriptive name or function summary which would allow us to identify the intent.

As a result of it being meant to return the largest element out of a collection, array size 0 should absolutely just throw an error and let whoever called it deal with that.

'What is the size of the largest apple in this empty basket?'
'Uhm, zero?'

There's a reason calculators throw an error when trying to divide by zero, and it's related to this problem, you just can't make some statements about an empty collection. 0 is just as wrong of an answer as 3 or -2,12123.

1

u/SnugglyCoderGuy Oct 15 '26

It does doesn't it. I misread return <el> as return <one> so big spologies there. That changes everything. My bad. I'm sorry.

2

u/Marisakis Oct 15 '26

I think we can absolutely agree that the font used is horrible. :D

And I've also slightly changed my mind towards that there could be educational value anyway without adhering to strict basics like perfect correctnesss (instead of left as an excercise to the reader), but yeah.. hehe.

1

u/YoungMaleficent9068 Oct 15 '26

Suddenly the coder guy is snuggly. Giving me all the beef lol.

0

u/YoungMaleficent9068 Oct 15 '26

I'm just gonna sit here and be happy that my prof cooked better 🤠 piece out

1

u/MisterMeme01 Oct 15 '26

peace*

1

u/YoungMaleficent9068 Oct 15 '26

Whatever the autocorrect gods decide

1

u/Liam_Mercier Oct 15 '26

Horribly handled in what way? If the goal is "compute the sum of the array" and the array is empty, then returning zero makes sense to me. Maybe I would add some sort of debug mode assert if size zero input is unexpected.

0

u/YoungMaleficent9068 Oct 15 '26

Yes, we are calculating the sum. Alrighty.

1

u/[deleted] Oct 18 '26

[removed] — view removed comment

1

u/YoungMaleficent9068 Oct 18 '26

I have a bunch of issues with it. First it's part of the algorithm. you take that line away the entire loop is useless. So it's actually the only important line in all of it. You could teach this on an algorithmic level but has nothing to do with its instructions count.

So why would the instructions count of it matter? This is java but java is essentially pseudo code. The jit is hopefully and likely completely vectorising this. https://inside.java/2025/08/16/jvmls-hotspot-auto-vectorization/ *would need to verify Even if the jit or graal or whatever compiler lets this loop to the CPU l12 completely vanishes in relevance and the branching takes domanince of the entire loop and every moderns cpus branch predictor gonna predict the heck out of these branches. Only if you knew for your specific application that you gonna process arrays that are favourably shaped you can give a hint to the branch predictor. And you can teach all that if you don't want to teach the algorithmic stuff but want to count instructions.

But given what is happening here it's a weird mix of everything that is not useful in any educational process.

And I don't understand where you are coming from with the java streams example. That's just extra pseudo code the later layers will cook into something decent + a potential extra alloc for a stream object.

1

u/[deleted] Oct 18 '26 edited Oct 18 '26

[removed] — view removed comment

1

u/YoungMaleficent9068 Oct 19 '26

Im not reading all of this after realizing in paragraph 3 that you didn't understand my statement.

0

u/[deleted] Oct 20 '26 edited Oct 20 '26

[removed] — view removed comment

1

u/YoungMaleficent9068 Oct 20 '26

Username checks out I guess?

I mean yeah just because aws is the shittiest of the clouds I don't even believe there is a single max function in there that return 0 for empty collections.

As if you personally build aws, you have some fries to bag id say :-)

0

u/[deleted] Oct 21 '26 edited Oct 21 '26

[removed] — view removed comment

1

u/YoungMaleficent9068 Oct 21 '26

U keep hopping your staff engineer doesn't find this then :-)

→ More replies

186

u/JayMKMagnum Oct 14 '26

You're not being asked to pick a value of n. You're being asked to describe how, given a value of n, you could specify an array of size n that achieves the best-case complexity.

So if I asked you "Can you give me an example of an array of 5 elements on which this algorithm achieves its best time? How long does it take?", you wouldn't start going "well an array of zero elements takes constant time".

51

u/LitespeedClassic Oct 14 '26 edited Oct 14 '26

This ^^^ is the right interpretation of the question. Source: I'm an algorithms professor.

Question to OP: when you are asked to give best case vs. worst case, is your professor having you precisely count operations or just give the big-O class?

If you are trying to count an operation, think about when line 12 gets run. It's the only line conditioned for general input size n, so its the only place you can get savings in the total number of operations. That line only gets invoked when the next array object is larger than l. So a reverse sorted list will never hit line 12.

25

u/Witty-Play9499 Oct 14 '26

Even if the array was reverse sorted wouldn't this algorithm be O(n) / linear time because it still needs to traverse through every element in the array to determine this.

28

u/LitespeedClassic Oct 14 '26

Yes, that's why I asked if their professor is still having them count operations, or are they on to big-O. If you are counting operations, you'll get the least number (but still O(n)) in a reverse sorted array. If the class now only cares about big-O class, then this is a bad question, because all inputs of size n take the same runtime O(n).

ETA: it doesn't need to be reverse sorted. The first element just needs to be the max element, as u/First_Funny_9402 gave in their answer.

5

u/foxsimile Oct 15 '26

Do you ever touch on branching with your students?

5

u/Comp_Sci_Doc Oct 14 '26

Yes - the asymptotic run time here is always going to be O(n). But you can choose the input to minimize/maximize the number of steps.

This can matter in real-world situations; you could, for example, have an O(n^2) algorithm that's better than an O(n lg n) algorithm in practice, because the constant factor on the O(n^2) algorithm is small and the one on the O(n lg n) algorithm is large relative to the size of the input.

3

u/blugil Oct 14 '26

This is the correct interpretation of OPs question and asks the correct question back. My assumption is, since we’re not being asked about Big-Omega as a function of N directly, that OP would need to describe, in words, an array that causes the most # of operations vs least # of operations.

Also for OP, you do not consider what N’s value might be when talking about time complexity of an algorithm, you could have an algorithm that’s O(n!) and the best case scenario from your interpretation would be an array of size 0 or 1. Instead you’re looking to figure out properties of how the array “looks” and how that array could make an algorithm run at a better or worst case time complexity.

Look into Big Omega, Big Theta, and Big O notation if you’re curious for more information. That might also help you figure out what exactly makes sense for your assignment. However, I’m confident they just want to know how an array looks that runs the most # of instructions vs an array that runs the least.

0

u/[deleted] Oct 14 '26

[deleted] — view deleted comment

1

u/YoungMaleficent9068 Oct 14 '26

Tell me you never read Knuth without telling me you never read Knuth

1

u/frnzprf Oct 16 '26 edited Oct 16 '26

As I understand it, the algorithm would be fastest, if the largest number is at the beginning of the array and slowest, if it's ordered from small to large — because then it would have to assign a new l in every iteration of the loop.

This has little to do with O-complexity. The O-complexity is O(n) in any case.

(If the size of the array is 0, the loop isn't entered, but if the size of the array is 37, then there is also still a constant that is larger than the execution time, so you could say it's O(1). You really need a variable input to make a meaningful statement about time complexity.)

1

u/JayMKMagnum Oct 16 '26

Depends whether you're counting comparisons or assignments or both. If you're including comparisons, it's ~n operations no matter how the input is structured.

0

u/Witty-Play9499 Oct 14 '26

Somehow i read it as 'Describe the array that would make the algorithm in best case and describe another array that would make the algorithm perform its worst'

70

u/SufficientStudio1574 Oct 14 '26

Fuck this font, I read it 10 times and kept getting l and 1 mixed up.

10

u/fllthdcrb Oct 14 '26

Could be worse. They could be (aImost) compIeteIy indistinguishabIe like "I" and "l" are in some sans-serif fonts, and such a font could be getting used where those letters need to be distinguisable, as it sometimes is. Not that I'm saying this looks great.

28

u/MistakeIndividual690 Oct 14 '26

This is always O(n) however the fastest time may be when element 0 is the maximum, because you only take the branch once. However it’s possible that it’s not faster at all if it’s compiled to a branch-less assignment

7

u/pi621 Oct 14 '26

To add onto what other people have said
The function inside O describes the relationship between runtime and the value n.
O(1) means the runtime is constant regardless of n.
O(n) means the runtime scales linearly as n increases.

saying something like
"the best case would be O(1) because the array is size 1"
is a fundamental misunderstanding of what big O notation actually means, and you should revise either by yourself or with your professor.

you're essentially saying "Okay so in the best case, this function's runtime is the same for arbitrary size, because if you look at when the size is 0 or 1, the runtime is constant."

Doesn't make a lot of sense, does it?

11

u/rotuami Oct 14 '26

To be really pedantic, the question you pose in the title (what is the big-O complexity of the algorithm) is underspecified. Complexity analysis requires some input and some output. In this case, the implication is "input n is the size of the list" and "output is the number of instructions". But you could analyze, e.g. the number of assignments to l, and that would give you a different answer.

The early return is a distractor, because it's determined by n and so won't be 0 when the list is empty. The important thing to notice is that the number of steps the loop executes is independent of the contents of the list. So best case and worst case are the same - and any list of size n would be a correct answer. E.g. "a list of all zeroes" or "a list of the integers from 1 to n"

2

u/SirClueless Oct 14 '26

I agree, the question should really specify what is being measured:

  • Comparison operators? Always = n.
  • Assignments to l? Varies.
  • Number of statements evaluated? Varies, but always linear.
  • Virtual instructions as specified by the textbook? Varies, but always linear.
  • CPU instructions executed? Probably exactly = C*n + k for some C, k.
  • Runtime? Depends a lot on the compiler and architecture where this is running.

4

u/alpicola Oct 14 '26

It was probably a mistake for you to start talking Big-O notation right off the bat. So many people in this thread seem overly fixated on Big-O and are missing what the question has actually asked. This isn't a question about time complexity, it's a question about identifying "pathological" cases.

This function has a couple of trivial cases:

You will get the best performance if the array is empty (n == 0). In this case, the if statement (line 4) will will read true and the function will immediately return a constant (line 5).

You will get worse performance if the array has a single element (n == 1). In this case, the if statement (line 4) will read false and the function will:

  1. Perform an array lookup (line 8)
  2. Assign the resulting value to a variable (line 8)
  3. Initialize a new variable i (line 10)
  4. Perform the comparison i < n (line 10), which will read true
  5. Perform another array lookup (line 11)
  6. Perform the comparison between the looked up variable and l (line 11), which will read false
  7. Increment i (line 10)
  8. Perform the comparison i < n (line 10), which will read false
  9. Return a variable (line 16)

This function also has specific performance characteristics for sorted arrays:

If the array is sorted in ascending order, line 11 will always read true and line 12 will always happen. This is the worst case because it executes every line of the function (except line 5) the maximum possible number of times.

If the array is sorted in descending order, line 11 will always read false and line 12 will never happen. This is the best non-trivial case because it uses the entire function except for line 12.

Unsorted arrays will fall somewhere in between the ascending and descending order scenarios because line 12 will be executed some of the time but not all of the time. This is the average or expected case.

2

u/fllthdcrb Oct 14 '26

TBF, question (c) is explicitly about Big-O, so it does need to be addressed there. (OP put (b) and (c) in the text, instead of the image, so it's easy to miss them).

4

u/Newbane2_ Oct 15 '26

I don't really get the question, it's just finding the largest double in the array in O(n) time. I guess a sorted array in descending order would be best case (fewest number of assignments) and ascending order would be worst case (most number of assignments) although they would still both be O(n) since you need to iteration through every element in the array and do a comparison for every element.

6

u/First_Funny_9402 Oct 14 '26

So #1 figure out what this is doing, by the end of the algorithm l will contain the max value of the input array. Now for best/worst case see how many operations are done given an input. No matter what, a comparison is made between every value of the array and the current value of l. What changes based on the input is if l is updated. So a best case would be one where the first element is the max element of the input array, because l gets set to a[0] and then there is no a[i] greater than that value to cause l to be updated. The worst case is an array that is sorted in ascending order, where a[i+1]>a[i] for all i, causing l to be updated after the comparison at every step.

1

u/SLiV9 Oct 16 '26

This might be what the professor intended as the answer, but it really makes no sense to be teaching students to count number of operations in 2026. That's not how computers work.

 The worst case is an array that is sorted in ascending order, where a[i+1]>a[i] for all i, causing l to be updated after the comparison at every step.

With modern CPU's, I would say this is the second best case, just behind the array being sorted in descending order.

The worst case by far would be a random array of distinct numbers. Any array that maximizes the branch misprediction.

0

u/krimin_killr21 Oct 14 '26

But in all cases the same instructions are executed (this would be a cmov instruction I would think) so it doesn’t matter what the array is like, it will have the same runtime.

2

u/lostshelby Oct 14 '26

So you're being asked to describe the array, of size n, which produces the best and worst case. Now, if n was 0 then yes, you get a constant time, however that is not the question. To describe the array (of size n, not size 0) it needs to be something where it iterates over all n elements but doesn't really do much. This is a find max algorithm, which sets max as the first element then check all other n-1 elements. Ideally, the first element IS the max value and then it never needs to update the variable after that point. Consider the worst case scenario, the array values are in ascending order, meaning the max updates every time. Strictly speaking though they are both just linear.

2

u/Comp_Sci_Doc Oct 14 '26

O(1) means that the algorithm takes a constant amount of time to execute, regardless of the size of the input. You can't do that here, because if the array has any elements in it, you're going to loop over all of them. What you can do is select an input that will minimize/maximize the number of operations required for a given array size.

2

u/golfstreamer Oct 14 '26

The function goes through the same number of steps for all arrays of length n so the best case and worst case should be the same 

2

u/fllthdcrb Oct 14 '26

Depends on which operations you measure. It's O(n) comparisons either way, but the number of assignments depends on the order. I would think you wouldn't measure the assignments alone, but maybe more expensive operations could warrant this.

2

u/Responsible-Bar7165 Oct 15 '26 edited Oct 15 '26

which processor architecture / version ? if that's not specified then there's no way to answer the question.

on modern arm / x64 it won't make a difference what the values are, the branch predictor and out-of-order dispatch will eliminate all measurable differences, especially if the JIT can vectorize that loop.

1

u/alpha_hydrogen Oct 16 '26

Hey, what subject topics are those?

1

u/Responsible-Bar7165 Oct 16 '26

Computer architecture.

1

u/BlazeWolfEagle Oct 16 '26

"there's no way to answer" is crazy we literally have a very defined way to express this with mathematical precision.

Big O notation is and has been a pretty standard way to express this exact idea without knowing these details.

1

u/Remaked Oct 16 '26

This is going to be O(n) in any case and they‘re technically right about the branch predictor and all. OP for sure isn‘t at that stage yet in their studies, though.

1

u/Responsible-Bar7165 Oct 16 '26

The question doesn’t ask about different complexities, the algorithm is clearly O(n). The question asks about best/worst cases for different inputs of the same size. The only way that could make a difference is if the number of comparisons, branches or assignments depend on the values of the inputs and whether or not the processor running the code is able to execute those in parallel. Hence my answer.

1

u/not_some_username Oct 17 '26

Once the N is known, it’s become O(1) 😭

3

u/psicodelico6 Oct 14 '26

Best case: O(n)

1

u/SmokeMuch7356 28d ago

O(1) means constant time - it takes the same amount of time regardless of the input size.

This particular algorithm depends on the size of the array, so it's O(N).

1

u/enchantedtotem Oct 14 '26

if n is sufficient large it’s about O(n) (1 assign + (n-1) compare) for monotonic increasing array, and roughly O(2n) (n assigns and (n-1) compares) for monotonic decreasing array.

1

u/Heapifying Oct 14 '26

O-notation is a set of functions. When you say O(1), you actually mean O(x->1), where -> represents a mapping. For example O(n²) is actually O(n->n²), or well if you want, f(n)=n², then you rewrite it as O(f).

You are working with functions here. You usually declare T(n) = "the amount of primitive steps when the array has length n". Usually, you use comparisons (<) as the primitive you are counting. In this case T(0)=1, T(1)=1.

The question is then, is there a family of arrays of any length, such that T \in O(1) for any array in that family?

1

u/Rararror Oct 14 '26

arr[x] = y 
0 <= x <= n

For every n: 
y <=1 is best case 
y >1 is worst case

Complexity is always O(n)

1

u/WafflePeak Oct 14 '26

We always assume n is going to infinity for this sort of analysis, the best case is N

It could really only be constant in this case if line 12 was a break or something that would terminate the loop.

Also be careful about describing best case with O notation, it’s generally better to use Theta notation for it.

1

u/XNo_Notes Oct 14 '26

You need to give an array. Like what string of numbers would make this function work the shortest amount of time. So best case would be an empty array, worst case would be an array at size max full of 2s.

1

u/Neither_Berry_100 Oct 15 '26

Looks like it returns the max or zero if the array is empty. Checks every index exactly once. O(n). Always. No best or worst case. Technically it has some fixed overhead. So if the array is empty you have cost / 0 elements. = infinite cost per elements lol. But just O(n).

1

u/AmbitiousSet5 Oct 15 '26

Big O is asymptotic analysis, as n approaches infinity. Best case doesnt mean just choosing a specific n. Its always as n goes to infinity.

1

u/3inthecorner Oct 15 '26

If the first element is the largest, you avoid all the assignments to l and if it's in ascending order, you do all of them, but the actual worst case is when the branch predictor gets confused. How would you craft an array that each element has a 50% chance of being the largest so far?

1

u/0xt0bi03 Oct 15 '26

best case is always o(n) actually, it will go through the array no matter what.

1

u/amir650 Oct 16 '26

You only return after having traversed the entire array of N elements …

1

u/dnabre Oct 16 '26

Keep in mind that in general just because it asks for 'best case' and 'worst case' doesn't mean they are different. To what degree those being the same are a "trick question" is up for debate.

If you consider that you were initially thinking O(1) for array size 0 or 1, the question may be focusing on that incorrect understanding.

1

u/InTheHiggsField Oct 16 '26

Best case is the situation where arr[0] is the greatest value in the list. Because then the conditional on line 11 never resolves to true and thus we never do the assignment operation. The worst case is the situation where the array is in ascending order. Then on every element 11 will resolve to true and the assignment operation will occur.

1

u/Thick_Secretary_1528 Oct 17 '26

It is O(n) - linear. It needs to traverse across all values still.

1

u/goos_ Oct 17 '26

You have the right idea, only thing is the question is asking "array of size n" so they wanted an example of arbitrary n, not just n = 0 or n = 1.

1

u/j-joshua Oct 17 '26

Best case what?

If you're looking for big O notation... The function is O(n) for any input.

1

u/Agus_Merino Oct 18 '26

No tiene mucho sentido la pregunta. El algoritmo no tiene una condición de corte específica para una situación particular. El arreglo tiene que ser recorrido n veces bajo cualquier ejecución, no importa si está ordenado de entrada o completamente desordenado.

Alguien podría decir "bueno pero el costo de la operación de ejecutar la asignación (línea 12), bajo ciertas entradas, no se ejecuta". Si, es verdad, pero una asignación es O(1), ejecutarla o no es tan insignificante que se termina diluyendo en el total. Esto puede verse en cuanto tenemos un algoritmo que hace múltiples operaciones de diferentes complejidades y el cálculo de costo queda en algo como, por ejemplo: n + n^2. Este algoritmo inevitablemente es n^2 ya que la operación de orden n termina siendo "absorbida" por la de orden mayor.

1

u/plintervals Oct 18 '26

First of all, whoever wrote this question and name the variable l is Satan.

It's not asking you to change the size of the array. It's asking, given a fixed size n array, come up with an array that gives the best case performance, and an array that gives the worst case performance.

1

u/misingnoglic Oct 18 '26

That would be wrong, or at least misleading. If it was a function to search an array for a value, the best case would be that you find it immediately and return. But in this function you are always looking at every element. Big O is about how runtime grows in proportion to the input size, so the fact that this function returns immediately for an empty array doesn't do anything for you.

1

u/iam-samzz Oct 18 '26

It's completely based on input size. I think ur function returns the max value in the array.

So worst and best case will be O(N) in ur program.

0

u/MyOthrUsrnmIsABook Oct 15 '26

A lot of people are saying the worst case is if the list is sorted in ascending order, since that will cause l to be set the most times. I would argue a jagged ascending array where every other value is larger than any preceding value could be worse, since it would cause branch mispredictions more of the time than a sorted array.

-1

u/WittyStick Oct 14 '26

O(*) is worst-case complexity. The complexity of this function is O(n).

The question is asking what specific inputs would complete it constant time versus linear time - in that regard, input 0 will complete in constant time, but any other in linear time.

-9

u/MrKarat2697 Oct 14 '26

an empty array would simply return 0 and complete in O(1), you are right

6

u/JayMKMagnum Oct 14 '26

It's not really accurate to say that an algorithm is "O(1) for input of size zero". Big-O notation refers to how the complexity scales over various sizes. Even if that were a true statement, it's not an answer to the question the assignment raises.

1

u/MrKarat2697 Oct 14 '26

ohhh wait you're right my bad

4

u/[deleted] Oct 14 '26

"Describe an input array of size n that gives the best case..."

2

u/lifebringingh2o Oct 14 '26

No, because you can apply your same logic (i.e. fix a specific value of n) to literally any algorithm and claim that it's best case is O(1), which would make best case time complexity a meaningless metric. When one says best case what is typically meant is a family of inputs of size n=1,2,... ad infinitum that gives the best asymptotics

-2

u/Alex_1503 Oct 14 '26

It says to describe AN input array for the best and worst case, so I think the empty array for "best case" of size 0, idk. The big O is O(n), but best input array is the empty one, right?