I'm trying to find the time complexity for the following code.
N= number of elements in array D= a constant: D>1 V= a constant: V>1000 counter=1; //the maximum value of the counter is N/D. for(i=0; i<N; i++) { [OP1] O1_Operation; // O(1) operation. [Total: N times] [OP2] if(i%D!=0) continue; // O(1) operation. [Total: N times] [OP3] for(j=0;j<counter;j++) // [Total: {(N/D)*((N/D)+1)}/2 times] [OP4] for(s=0;s<V;s++) [OP5] O1_Operation; // O(1) operation. [Total: (V*{(N/D)*((N/D)+1)}/2) times] [OP6] counter++; // O(1) operation. [Total: N/D times] }
I added each operation time complexity and the total times it will be executed. The confusion for me in this code is because of the mod operation. This mod will allow only (N/D) operations to complete the code OP[3-6].
For [OP3] in the first time it will get execute 1 time, the second 2 times, ..., N/D times. Hence, the total number of executions can be [(N/D) * ( (N/D)+1)] /2. Removing D and V because they are constants will lead to a complexity of O(N^2) for the whole code.
Is this correct?