×
Stop thinking about WHAT WILL HAPPEN and start thinking about WHAT YOU CAN DO.

# Solution to Previous Year Exam Questions for Data Structure and Algorithms - DSA of KIIT by Abhishek Apoorv

• Data Structure and Algorithms - DSA
• 2016
• PYQ Solution
• Kalinga Institute of Industrial Technology - KIIT
• Computer Science Engineering
• B.Tech
• 575 Views
Page-1

#### Solution to Previous Year Exam Questions for Data Structure and Algorithms - DSA of KIIT by Abhishek Apoorv / 30

0 User(s)

#### Text from page-2

School of Computer Engineering AUTUMN 2016 KIIT, UNIVERSITY DSA Questions & Answers Data Structure & Algorithm n=n2log n times. Simply rule to remember: (concentrate on loop variable) a) Time complexity of a function (or set of statements) is considered as O(1) if it doesn’t contain loop, recursion and call to any other non-constant time function. Example: void swap(int *x, int *y) { int temp; temp=*x; *x=*y; *y=temp; } Time complexity of swap function= Total number of simple statements=4=constant=O1) b) A loop or recursion that runs a constant number of times is also considered as O(1). Example // Here c is a constant int i; for (i = 1; i <= c; i++) { // some O(1) expressions } c) Time Complexity of a loop is considered as O(n) if the loop variables i is incremented / decremented by a constant amount c. Example: for (i = 1; i <= n; i=i+c) for (i = n; i <= 1; i = i-c) { { // some O(1) expressions // some O(1) expressions } } d) Time Complexity of a loop is considered as O(log n) if the loop variables i is multiplied or divided by a constant amount c. Example: for (i = 1; i <= n; i=i*c) for (i = n; i <= 1; i = i/c) { { // some O(1) expressions // some O(1) expressions } } e) Time Complexity of a loop is considered as O(log log n) if the loop variables i if the loop variables is reduced / increased exponentially by a constant amount c. Example: // Here c is a constant greater than 1 //Here fun is sqrt or cuberoot or any for (i = 2; i <= n; i=pow(i,c)) other constant root { for (i = n; i > 0; i = fun(i)) KIIT/SCE/AUTUMN-2016/3rdSem/CS-10/DSA/CS-2001/Study Material by Prof. Anil Kumar Swain Page 2 of 30

#### Text from page-3

School of Computer Engineering AUTUMN 2016 KIIT, UNIVERSITY DSA Questions & Answers // some O(1) expressions Data Structure & Algorithm { } // some O(1) expressions } f) Time complexity of nested loops is equal to the number of times the innermost statement is executed that is nothing but the multiplication of outer loop complexity into inner loop complexity. Sl. Program/Program Segment Time Explanation No. Complexity 1 int i, j, k; O(m*n) Outer for m times and inner for n for (i = 1; i <=m; i ++) times, so total mn times { If m=n, then for (j = 1; j <=n; j ++) { O(n2) k=k+1; } } 2 int i, j, k; O(n2) Outer loop approx n times and for (i = n; i >1; i =i-2) inner approx n times, so total n2 { times. for (j = 1; j <=n; j=j+3) { k=k+1; } } 3 int i, j, k; O(nlog n) Outer loop approx log n times, for (i = n; i >1; i =i*2) inner loop n times, so total nlog n { times. for (j = 1; j <=n; j++) { k=k+1; } } 4 for (i = 1; i<=n; i=i*2) O((log n)2) Outer loop approx log n times and { inner loop approx log n times, so for (j =n; j>=1; j=j/2) total ((log n)2 times. { Simple-Statements; } } 5 for (i = 1; i<=n; i++) O(n) The outer loop executes approx. n { times, and the innermost 100 times. for (j =1; j<=100; j++) Here { n=O(n) and 100=O(1) constant ime Simple-Statements; So total=O(n)*O(1)=O(n) } } KIIT/SCE/AUTUMN-2016/3rdSem/CS-10/DSA/CS-2001/Study Material by Prof. Anil Kumar Swain Page 3 of 30

#### Text from page-4

School of Computer Engineering AUTUMN 2016 KIIT, UNIVERSITY DSA Questions & Answers Data Structure & Algorithm g) When there are consecutive loops, the time complexity is calculated as sum of time complexities of individual loops and final time complexity is the higher order term in terms of n (the one which is larger than others for larger value of n) Sl. Program/Program Segment Time Explanation No. Complexity 1 for (int i = 1; i <=m; i += c) O(m+n) First for-I loop O(m), second for-i { If m=n, then loop O(n) times, so total O(m+n) // some O(1) expressions O(2n)=O(n) times } for (int i = 1; i <=n; i += c) { // some O(1) expressions } 2 for (int i = 1; i <=n; i *= 2) O(n) First for-I log n times, second n { times. Now total=log n+n =O(n) as // some O(1) expressions n is asymptotically larger than log } n. for (int i = 1; i <=n; i ++) { // some O(1) expressions } 3 int i, j, k; O(nlog n) Nested for-ij loop is executed nlog for (i = 1; i <=n; i ++) n times. For k loop log n times, for{ l loop n times. So total=nlog n + for (j = 1; j <=n; j=j*2) log n + n = O(nlog n) as nlog { n>n>log n p=i+j; } } for ( k = n; k >=1; k=k/3) { q=k+p; } for ( l = n; l >=1; l=l-2) { q=k+p; } KIIT/SCE/AUTUMN-2016/3rdSem/CS-10/DSA/CS-2001/Study Material by Prof. Anil Kumar Swain Page 4 of 30