Consider the procedure . procedure Loops2 (n, a positive integer) 1. for k = 1 t
ID: 3604212 • Letter: C
Question
Consider the procedure .
procedure Loops2 (n, a positive integer) 1.
for k = 1 to nn -2 .
for i = k + 1 to n -1 .
for j = i + 1 to n .
print (i, j) i.
i.In terms of n, how many ordered pairs are printed?
ii. What is the runtime in notation of this procedure?
Consider the procedure procedure Loops2 (n, a positive integer) 1. for k 1 to n-2 2. for k+1 to n- 1 3. 4. for j i + l to n print (i.) i. In terms of n, how many ordered pairs are printed? ii. What is the runtime in notation of this procedure?Explanation / Answer
Let me know if you have any doubts.
Solution i) (n-2)*(n-2)*(n-2)
Explanation:
procedure Loops2 (n, a positive integer)
for k = 1 to n -2 => this will run from 1 to (n-2) times, So total (n-2) times
for i = k + 1 to n -1 => this will run from 2 to (n-1) times, So total (n-2) times
for j = i + 1 to n => this will run from 3 to (n) times, So total (n-2) times
print (i, j) => Hence, Total number of times are: (n-2)*(n-2)*(n-2)
Solution ii) Runtime complexity: O(n3)
Explanation:
=> (n-2)*(n-2)*(n-2)
=> n3 ( 2 is constant)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.