1.

(i) What is the worst-case complexity of the follow ing code segment:for (int i = 0; i < N; i++) { sequence of statements } for (int j=0 ; j < M; j++) {sequence of statements } (ii) How would the complexity change if the second loop went to N instead of M?

Answer»

(i) for (i = 0; i < N ; i + +) This loop gets executed N times thus take time C1 * N for (i = 0; j < M ; j ++) This loop gets executed M times thus take time C2 * M Total Time = C1 * N + C2 * M = 0 (N + M) 

(ii) It becomes = O(2N)



Discussion

No Comment Found

Related InterviewSolutions