Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

determine complexity class [closed]

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;

}
like image 859
cluv Avatar asked May 17 '26 00:05

cluv


1 Answers

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:

  • 2n multiplications
  • 2n additions
  • 2n divitions
  • n modulo operations

This is not counting the arithmetic operations for implementing the for loop.

like image 89
user000001 Avatar answered May 18 '26 14:05

user000001



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!