I am learning Big O notation in my algorithms class. I just done an online quiz for this week where we get a bunch of code blocks which we have to select the big O complexity for.
I got everything correct except this code block:
void printPairs(int[] arrX, int[] arrY) {
for(int i = 0; i < arrX.length; i++) {
for(int j = 0; j < arrY.length; j++) {
System.out.println(arrX[i] + "," + arrY[j]);
}
}
}
I put in O(N^2)
but I got it wrong which I am not sure why, there are two for-loops? And unfortunately I can not see the other options available or correct answer till the end of the week.
The runtime would be O(N²) if there was only one input i.e. one array passed as a parameter and iterated over in each of the two for-loops.
Because there are two inputs (arrX
and arrY
) for the method and both are used in each of the two for-loops.
The Big O runtime is O(XY) where X = arrX.length
and Y = arrY.length
.
Edit: As stated by @Oighea This is still a quadratic-time algorithm. It is just in two variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With