Consider the following algorithm:
j = 1 ;
while ( j <= n/2) {
i = 1 ;
while ( i <= j ) {
cout << j << i ;
i++;
}
j++;
}
(a) What is the output when n = 6, n = 8, and n = 10?
(b) What is the time complexity T(n)? You may assume that the input n is divisible by 2.

Answer Posted / gokul s

n=6:
(1, 1)
(2, 1)
(2, 2)
(3, 1)
(3, 2)
(3, 3)

n=8:
(1, 1)
(2, 1)
(2, 2)
(3, 1)
(3, 2)
(3, 3)
(4, 1)
(4, 2)
(4, 4)
(4, 4)

n=10:
(1, 1)
(2, 1)
(2, 2)
(3, 1)
(3, 2)
(3, 3)
(4, 1)
(4, 2)
(4, 3)
(4, 4)
(5, 1)
(5, 2)
(5, 3)
(5, 4)
(5, 5)


b) Time complexity is ((n/2) * (n/2)+1)/2

Is This Answer Correct ?    2 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can you list some use cases where classification machine learning algorithms can be used?

530


What is the bidirectional search algorithm?

571


Which is the most straight forward approach for planning algorithms?

599


What features would you use to build a recommendation algorithm for users?

588


what is software cycle?give a diagramatic representation

2396






What are the disadvantages of depth-first search algorithm?

558


What is the most efficient way to store 1 million phone numbers memory-wise?

1336


What’s your favorite algorithm, & can you explain it into me in less than a minute?

815


Can you pick an algorithm. Write the psuedo-code for a parallel implementation?

521


How are the k-nearest neigh-bors (knn) algorithms different from k-means clustering?

537


Sorting algorithms and time complexity of various algorithms.

587


What is greedy best first search algorithm?

593


What are the complexities of sorting algorithms and how can this complexity be calculated?

621


Which search algorithm will use a limited amount of memory in online search?

732


Explain the Canny's Algorithm.

588