What is the big o notation here? An explanation would be appreciated. Thanks.
public static int[] mystery1(int[] list) {
int[] result = new int[2 * list.length];
for (int i = 0; i < list.length; i++) {
result[2 * i] = list[i] / 2 + list[i] % 2;
result[2 * i + 1] = list[i] / 2;
}
return result;
}
It is
O(n), where n is the length of the list. You go through the entire list once in any case.The number of arithmetic operations are:
This is not counting the arithmetic operations for implementing the for loop.