java - Big O for 3 nested for loops? -


public int loop(int[] array1) {         int result = 0;         (int = 0; < array1.length; i++) {             (int j = 0; j < array1.length; j++) {                 (int k = 1; k < array1.length; k = k * 2) {                     result += j * j * array1[k] + array1[i] + array1[j];                 }             }         }         return result;     } 

i'm trying find complexity function counts number of arithmetic operations here. know complexity class o(n^3), i'm having bit of trouble counting steps.

my reasoning far count number of arithmetic operations 8, complexity function 8n^3?

any guidance in right direction appreciated, thank you!

the first loop run n times, second loop run n times third loop run log(n) times (base 2). since multiplying k 2 each time inverse operation take log. multiplying have o(n^2 log(n))


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -