# Multiple choice question for engineering

## Set 1

1. Which of the following methods can be used to solve the edit distance problem?

a) Recursion

b) Dynamic programming

c) Both dynamic programming and recursion

d) None of the mentioned

### View Answer

2. The edit distance satisfies the axioms of a metric when the costs are non-negative.

a) True

b) False

### View Answer

3. Which of the following is an application of the edit distance problem?

a) Approximate string matching

b) Spelling correction

c) Similarity of DNA

d) All of the mentioned

### View Answer

4. In which of the following cases will the edit distance between two strings be zero?

a) When one string is a substring of another

b) When the lengths of the two strings are equal

c) When the two strings are equal

d) The edit distance can never be zero

### View Answer

5. Suppose each edit (insert, delete, replace) has a cost of one. Then, the maximum edit distance cost between the two strings is equal to the length of the larger string.

a) True

b) False

### View Answer

6. Consider the strings “monday” and “tuesday”. What is the edit distance between the two strings?

a) 3

b) 4

c) 5

d) 6

### View Answer

7. Consider the two strings “”(empty string) and “abcd”. What is the edit distance between the two strings?

a) 0

b) 4

c) None of the mentioned

d) Cannot be determined

### View Answer

8. Consider the following dynamic programming implementation of the edit distance problem:

#include<stdio.h> #include<string.h> int get_min(int a, int b) { if(a < b) return a; return b; } int edit_distance(char *s1, char *s2) { int len1,len2,i,j,min; len1 = strlen(s1); len2 = strlen(s2); int arr[len1 + 1][len2 + 1]; for(i = 0;i <= len1; i++) arr[i][0] = i; for(i = 0; i <= len2; i++) arr[0][i] = i; for(i = 1; i <= len1; i++) { for(j = 1; j <= len2; j++) { min = get_min(arr[i-1][j],arr[i][j-1]) + 1; if(s1[i - 1] == s2[j - 1]) { if(arr[i-1][j-1] < min) min = arr[i-1][j-1]; } else { if(arr[i-1][j-1] + 1 < min) min = arr[i-1][j-1] + 1; } _____________; } } return arr[len1][len2]; } int main() { char s1[] = "abcd", s2[] = "defg"; int ans = edit_distance(s1, s2); printf("%d",ans); return 0; }

Which of the following lines should be added to complete the above code?

a) arr[i-1][j] = min

b) arr[i][j-1] = min

c) arr[i-1][j-1] = min

d) arr[i][j] = min

### View Answer

9. What is the time complexity of the above dynamic programming implementation of the edit distance problem where “m” and “n” are the lengths of two strings?

a) O(1)

b) O(m + n)

c) O(mn)

d) None of the mentioned

### View Answer

10. What is the space complexity of the above dynamic programming implementation of the edit distance problem where “m” and “n” are the lengths of the two strings?

a) O(1)

b) O(m + n)

c) O(mn)

d) None of the mentioned

### View Answer

11. What is the output of the following code?

#include<stdio.h> #include<string.h> int get_min(int a, int b) { if(a < b) return a; return b; } int edit_distance(char *s1, char *s2) { int len1,len2,i,j,min; len1 = strlen(s1); len2 = strlen(s2); int arr[len1 + 1][len2 + 1]; for(i = 0;i <= len1; i++) arr[i][0] = i; for(i = 0; i <= len2; i++) arr[0][i] = i; for(i = 1; i <= len1; i++) { for(j = 1; j <= len2; j++) { min = get_min(arr[i-1][j],arr[i][j-1]) + 1; if(s1[i - 1] == s2[j - 1]) { if(arr[i-1][j-1] < min) min = arr[i-1][j-1]; } else { if(arr[i-1][j-1] + 1 < min) min = arr[i-1][j-1] + 1; } arr[i][j] = min; } } return arr[len1][len2]; } int main() { char s1[] = "abcd", s2[] = "defg"; int ans = edit_distance(s1, s2); printf("%d",ans); return 0; }

a) 1

b) 2

c) 3

d) 4

### View Answer

12. What is the value stored in arr[2][2] when the following code is executed?

#include<stdio.h> #include<string.h> int get_min(int a, int b) { if(a < b) return a; return b; } int edit_distance(char *s1, char *s2) { int len1,len2,i,j,min; len1 = strlen(s1); len2 = strlen(s2); int arr[len1 + 1][len2 + 1]; for(i = 0;i <= len1; i++) arr[i][0] = i; for(i = 0; i <= len2; i++) arr[0][i] = i; for(i = 1; i <= len1; i++) { for(j = 1; j <= len2; j++) { min = get_min(arr[i-1][j],arr[i][j-1]) + 1; if(s1[i - 1] == s2[j - 1]) { if(arr[i-1][j-1] < min) min = arr[i-1][j-1]; } else { if(arr[i-1][j-1] + 1 < min) min = arr[i-1][j-1] + 1; } arr[i][j] = min; } } return arr[len1][len2]; } int main() { char s1[] = "abcd", s2[] = "defg"; int ans = edit_distance(s1, s2); printf("%d",ans); return 0; }

a) 1

b) 2

c) 3

d) 4

### View Answer

13. What is the output of the following code?

#include<stdio.h> #include<string.h> int get_min(int a, int b) { if(a < b) return a; return b; } int edit_distance(char *s1, char *s2) { int len1,len2,i,j,min; len1 = strlen(s1); len2 = strlen(s2); int arr[len1 + 1][len2 + 1]; for(i = 0;i <= len1; i++) arr[i][0] = i; for(i = 0; i <= len2; i++) arr[0][i] = i; for(i = 1; i <= len1; i++) { for(j = 1; j <= len2; j++) { min = get_min(arr[i-1][j],arr[i][j-1]) + 1; if(s1[i - 1] == s2[j - 1]) { if(arr[i-1][j-1] < min) min = arr[i-1][j-1]; } else { if(arr[i-1][j-1] + 1 < min) min = arr[i-1][j-1] + 1; } arr[i][j] = min; } } return arr[len1][len2]; } int main() { char s1[] = "pqrstuv", s2[] = "prstuv"; int ans = edit_distance(s1, s2); printf("%d",ans); return 0; }

a) 1

b) 2

c) 3

d) 4

### View Answer

## Set 2

1. The following sequence is a fibonacci sequence:

0, 1, 1, 2, 3, 5, 8, 13, 21,…..

Which technique can be used to get the nth fibonacci term?

a) Recursion

b) Dynamic programming

c) A single for loop

d) All of the mentioned

### View Answer

2. Consider the recursive implementation to find the nth fibonacci number:

int fibo(int n) if n <= 1 return n return __________

Which line would make the implementation complete?

a) fibo(n) + fibo(n)

b) fibo(n) + fibo(n – 1)

c) fibo(n – 1) + fibo(n + 1)

d) fibo(n – 1) + fibo(n – 2)

### View Answer

3. What is the time complexity of the recursive implementation used to find the nth fibonacci term?

a) O(1)

b) O(n^{2})

c) O(n!)

d) Exponential

### View Answer

^{k}* T(n – k) This recurrence will stop when n – k = 0 i.e. n = k Therefore, T(n) = 2

^{n}* O(0) = 2

^{n}Hence, it takes exponential time. It can also be proved by drawing the recursion tree and counting the number of leaves.

4. Suppose we find the 8th term using the recursive implementation. The arguments passed to the function calls will be as follows:

fibonacci(8) fibonacci(7) + fibonacci(6) fibonacci(6) + fibonacci(5) + fibonacci(5) + fibonacci(4) fibonacci(5) + fibonacci(4) + fibonacci(4) + fibonacci(3) + fibonacci(4) + fibonacci(3) + fibonacci(3) + fibonacci(2) : : :

Which property is shown by the above function calls?

a) Memoization

b) Optimal substructure

c) Overlapping subproblems

d) Greedy

### View Answer

5. What is the output of the following program?

#include<stdio.h> int fibo(int n) { if(n<=1) return n; return fibo(n-1) + fibo(n-2); } int main() { int r = fibo(50000); printf("%d",r); return 0; }

a) 1253556389

b) 5635632456

c) Garbage value

d) Runtime error

e) Compile time error

### View Answer

^{50000}times. Now, even though NO variables are stored by the function, the space required to store the addresses of these function calls will be enormous. Stack memory is utilized to store these addresses and only a particular amount of stack memory can be used by any program. So, after a certain function call, no more stack space will be available and it will lead to stack overflow causing runtime error.

6. What is the space complexity of the recursive implementation used to find the nth fibonacci term?

a) O(1)

b) O(n)

c) O(n^{2})

d) O(n^{3})

### View Answer

7. Consider the following code to find the nth fibonacci term:

int fibo(int n) if n == 0 return 0 else prevFib = 0 curFib = 1 for i : 1 to n-1 nextFib = prevFib + curFib __________ __________ return curFib

Complete the above code.

a) prevFib = curFib

curFib = curFib

b) prevFib = nextFib

curFib = prevFib

c) prevFib = curFib

curFib = nextFib

d) none of the mentioned

### View Answer

8. What is the time complexity of the ABOVE for loop method used to compute the nth fibonacci term ?

a) O(1)

b) O(n)

c) O(n^{2})

d) Exponential

### View Answer

9. What is the space complexity of the ABOVE for loop method used to compute the nth fibonacci term?

a) O(1)

b) O(n)

c) O(n^{2})

d) Exponential

### View Answer

10. What will be the output when the following code is executed?

#include<stdio.h> int fibo(int n) { if(n==0) return 0; int i; int prevFib=0,curFib=1; for(i=1;i<=n-1;i++) { int nextFib = prevFib + curFib; prevFib = curFib; curFib = nextFib; } return curFib; } int main() { int r = fibo(10); printf("%d",r); return 0; }

a) 34

b) 55

c) Compile error

d) Runtime error

### View Answer

11. Consider the following code to find the nth fibonacci term using dynamic programming:

1.int fibo(int n) 2. int fibo_terms[100000] //arr to store the fibonacci numbers 3. fibo_terms[0] = 0 4. fibo_terms[1] = 1 5. 6. for i: 2 to n 7. fibo_terms[i] = fibo_terms[i - 1] + fibo_terms[i - 2] 8. 9. return fibo_terms[n]

Which property is shown by line 7 of the above code?

a) Optimal substructure

b) Overlapping subproblems

c) Both overlapping subproblems and optimal substructure

d) None of the mentioned

### View Answer

12. Consider the following code to find the nth fibonacci term using dynamic programming:

1.int fibo(int n) 2. int fibo_terms[100000] //arr to store the fibonacci numbers 3. fibo_terms[0] = 0 4. fibo_terms[1] = 1 5. 6. for i: 2 to n 7. fibo_terms[i] = fibo_terms[i - 1] + fibo_terms[i - 2] 8. 9. return fibo_terms[n]

Which technique is used by line 7 of the above code?

a) Greedy

b) Recursion

c) Memoization

d) None of the mentioned

### View Answer

13. What is the time complexity of the ABOVE dynamic programming implementation used to compute the nth fibonacci term?

a) O(1)

b) O(n)

c) O(n^{2})

d) Exponential

### View Answer

14. What is the space complexity of the ABOVE dynamic programming implementation used to compute the nth fibonacci term?

a) O(1)

b) O(n)

c) O(n^{2})

d) Exponential

### View Answer

15. What will be the output when the following code is executed?

#include<stdio. int fibo(int n) { int i; int fibo_terms[100]; fibo_terms[0]=0; fibo_terms[1]=1; for(i=2;i<=n;i++) fibo_terms[i] = fibo_terms[i-2] + fibo_terms[i-1]; return fibo_terms[n]; } int main() { int r = fibo(8); printf("%d",r); return 0; }

a) 34

b) 55

c) Compile error

d) 21

### View Answer

## Set 3

1. Kadane’s algorithm is used to find ____________

a) Longest increasing subsequence

b) Longest palindrome subsequence

c) Maximum sub-array sum

d) All of the mentioned

### View Answer

2. Kadane’s algorithm uses which of the following techniques?

a) Divide and conquer

b) Dynamic programming

c) Recursion

d) All of the mentioned

### View Answer

3. For which of the following inputs would Kadane’s algorithm produce the CORRECT output?

a) {0,1,2,3}

b) {-1,0,1}

c) {-1,-2,-3,0}

d) All of the mentioned

### View Answer

4. For which of the following inputs would Kadane’s algorithm produce a WRONG output?

a) {1,0,-1}

b) {-1,-2,-3}

c) {1,2,3}

d) {0,0,0}

### View Answer

5. Complete the following code for Kadane’s algorithm:

#include<stdio.h> int max_num(int a, int b) { if(a > b) return a; return b; } int kadane_algo(int *arr, int len) { int ans, sum, idx; ans =0; sum =0; for(idx =0; idx < len; idx++) { sum = max_num(0,sum + arr[idx]); ans = ___________; } return ans; } int main() { int arr[] = {-2, -3, 4, -1, -2, 1, 5, -3},len=7; int ans = kadane_algo(arr,len); printf("%d",ans); return 0; }

a) max_num(sum, sum + arr[idx])

b) sum

c) sum + arr[idx].

d) max_num(sum,ans)

### View Answer

6. What is the time complexity of Kadane’s algorithm?

a) O(1)

b) O(n)

c) O(n^{2})

d) None of the mentioned

### View Answer

7. What is the space complexity of Kadane’s algorithm?

a) O(1)

b) O(n)

c) O(n^{2})

d) None of the mentioned

### View Answer

8. What is the output of the following implementation of Kadane’s algorithm?

#include<stdio.h> int max_num(int a, int b) { if(a > b) return a; return b; } int kadane_algo(int *arr, int len) { int ans, sum, idx; ans =0; sum =0; for(idx =0; idx < len; idx++) { sum = max_num(0,sum + arr[idx]); ans = max_num(sum,ans); } return ans; } int main() { int arr[] = {2, 3, -3, -1, 2, 1, 5, -3}, len = 8; int ans = kadane_algo(arr,len); printf("%d",ans); return 0; }

a) 6

b) 7

c) 8

d) 9

### View Answer

9. What is the output of the following implementation of Kadane’s algorithm?

#include<stdio.h> int max_num(int a, int b) { if(a > b) return a; return b; } int kadane_algo(int *arr, int len) { int ans, sum, idx; ans =0; sum =0; for(idx =0; idx < len; idx++) { sum = max_num(0,sum + arr[idx]); ans = max_num(sum,ans); } return ans; } int main() { int arr[] = {-2, -3, -3, -1, -2, -1, -5, -3},len = 8; int ans = kadane_algo(arr,len); printf("%d",ans); return 0; }

a) 1

b) -1

c) -2

d) None of the mentioned

### View Answer

10. Consider the following implementation of Kadane’s algorithm:

1. #include<stdio.h> 2. int max_num(int a, int b) 3. { 4. if(a > b) 5. return a; 6. return b; 7. } 8. int kadane_algo(int *arr, int len) 9. { 10. int ans = 0, sum = 0, idx; 11. for(idx =0; idx < len; idx++) 12. { 13. sum = max_num(0,sum + arr[idx]); 14. ans = max_num(sum,ans); 15. } 16. return ans; 17. } 18. int main() 19. { 20. int arr[] = {-2, -3, -3, -1, -2, -1, -5, -3},len = 8; 21. int ans = kadane_algo(arr,len); 22. printf("%d",ans); 23. return 0; 24. }

What changes should be made to the Kadane’s algorithm so that it produces the right output even when all array elements are negative?

Change 1 = Line 10: int sum = arr[0], ans = arr[0] Change 2 = Line 13: sum = max_num(arr[idx],sum+arr[idx])

a) Only Change 1 is sufficient

b) Only Change 2 is sufficient

c) Both Change 1 and Change 2 are necessary

d) None of the mentioned

### View Answer

## Set 4

1. Which of the following methods can be used to solve the longest common subsequence problem?

a) Recursion

b) Dynamic programming

c) Both recursion and dynamic programming

d) None of the mentioned

### View Answer

2. Consider the strings “PQRSTPQRS” and “PRATPBRQRPS”. What is the length of the longest common subsequence?

a) 9

b) 8

c) 7

d) 6

### View Answer

3. Which of the following problems can be solved using the longest subsequence problem?

a) Longest increasing subsequence

b) Longest palindromic subsequence

c) Longest bitonic subsequence

d) None of the mentioned

### View Answer

4. Longest common subsequence is an example of ____________

a) Greedy algorithm

b) 2D dynamic programming

c) 1D dynamic programming

d) Divide and conquer

### View Answer

5. What is the time complexity of the brute force algorithm used to find the longest common subsequence?

a) O(n)

b) O(n^{2})

c) O(n^{3})

d) O(2^{n})

### View Answer

^{n}).

6. Consider the following dynamic programming implementation of the longest common subsequence problem:

#include<stdio.h> #include<string.h> int max_num(int a, int b) { if(a > b) return a; return b; } int lcs(char *str1, char *str2) { int i,j,len1,len2; len1 = strlen(str1); len2 = strlen(str2); int arr[len1 + 1][len2 + 1]; for(i = 0; i <= len1; i++) arr[i][0] = 0; for(i = 0; i <= len2; i++) arr[0][i] = 0; for(i = 1; i <= len1; i++) { for(j = 1; j <= len2; j++) { if(str1[i-1] == str2[j - 1]) ______________; else arr[i][j] = max_num(arr[i - 1][j], arr[i][j - 1]); } } return arr[len1][len2]; } int main() { char str1[] = " abcedfg", str2[] = "bcdfh"; int ans = lcs(str1,str2); printf("%d",ans); return 0; }

Which of the following lines completes the above code?

a) arr[i][j] = 1 + arr[i][j].

b) arr[i][j] = 1 + arr[i – 1][j – 1].

c) arr[i][j] = arr[i – 1][j – 1].

d) arr[i][j] = arr[i][j].

### View Answer

7. What is the time complexity of the above dynamic programming implementation of the longest common subsequence problem where length of one string is “m” and the length of the other string is “n”?

a) O(n)

b) O(m)

c) O(m + n)

d) O(mn)

### View Answer

8. What is the space complexity of the above dynamic programming implementation of the longest common subsequence problem where length of one string is “m” and the length of the other string is “n”?

a) O(n)

b) O(m)

c) O(m + n)

d) O(mn)

### View Answer

9. What is the output of the following code?

#include<stdio.h> #include<string.h> int max_num(int a, int b) { if(a > b) return a; return b; } int lcs(char *str1, char *str2) { int i,j,len1,len2; len1 = strlen(str1); len2 = strlen(str2); int arr[len1 + 1][len2 + 1]; for(i = 0; i <= len1; i++) arr[i][0] = 0; for(i = 0; i <= len2; i++) arr[0][i] = 0; for(i = 1; i <= len1; i++) { for(j = 1; j <= len2; j++) { if(str1[i-1] == str2[j - 1]) arr[i][j] = 1 + arr[i - 1][j - 1]; else arr[i][j] = max_num(arr[i - 1][j], arr[i][j - 1]); } } return arr[len1][len2]; } int main() { char str1[] = "hbcfgmnapq", str2[] = "cbhgrsfnmq"; int ans = lcs(str1,str2); printf("%d",ans); return 0; }

a) 3

b) 4

c) 5

d) 6

### View Answer

10. Which of the following is the longest common subsequence between the strings “hbcfgmnapq” and “cbhgrsfnmq” ?

a) hgmq

b) cfnq

c) bfmq

d) all of the mentioned

### View Answer

11. What is the value stored in arr[2][3] when the following code is executed?

#include<stdio.h> #include<string.h> int max_num(int a, int b) { if(a > b) return a; return b; } int lcs(char *str1, char *str2) { int i,j,len1,len2; len1 = strlen(str1); len2 = strlen(str2); int arr[len1 + 1][len2 + 1]; for(i = 0; i <= len1; i++) arr[i][0] = 0; for(i = 0; i <= len2; i++) arr[0][i] = 0; for(i = 1; i <= len1; i++) { for(j = 1; j <= len2; j++) { if(str1[i-1] == str2[j - 1]) arr[i][j] = 1 + arr[i - 1][j - 1]; else arr[i][j] = max_num(arr[i - 1][j], arr[i][j - 1]); } } return arr[len1][len2]; } int main() { char str1[] = "hbcfgmnapq", str2[] = "cbhgrsfnmq"; int ans = lcs(str1,str2); printf("%d",ans); return 0; }

a) 1

b) 2

c) 3

d) 4

### View Answer

12. What is the output of the following code?

#include<stdio.h> #include<string.h> int max_num(int a, int b) { if(a > b) return a; return b; } int lcs(char *str1, char *str2) { int i,j,len1,len2; len1 = strlen(str1); len2 = strlen(str2); int arr[len1 + 1][len2 + 1]; for(i = 0; i <= len1; i++) arr[i][0] = 0; for(i = 0; i <= len2; i++) arr[0][i] = 0; for(i = 1; i <= len1; i++) { for(j = 1; j <= len2; j++) { if(str1[i-1] == str2[j - 1]) arr[i][j] = 1 + arr[i - 1][j - 1]; else arr[i][j] = max_num(arr[i - 1][j], arr[i][j - 1]); } } return arr[len1][len2]; } int main() { char str1[] = "abcd", str2[] = "efgh"; int ans = lcs(str1,str2); printf("%d",ans); return 0; }

a) 3

b) 2

c) 1

d) 0

### View Answer

## Set 5

1. The longest increasing subsequence problem is a problem to find the length of a subsequence from a sequence of array elements such that the subsequence is sorted in increasing order and it’s length is maximum. This problem can be solved using __________

a) Recursion

b) Dynamic programming

c) Brute force

d) All of the mentioned

### View Answer

2. Find the longest increasing subsequence for the given sequence:

{10, -10, 12, 9, 10, 15, 13, 14}

a) {10, 12, 15}

b) {10, 12, 13, 14}

c) {-10, 12, 13, 14}

d) {-10, 9, 10, 13, 14}

### View Answer

3. Find the length of the longest increasing subsequence for the given sequence:

{-10, 24, -9, 35, -21, 55, -41, 76, 84}

a) 5

b) 4

c) 3

d) 6

### View Answer

4. For any given sequence, there will ALWAYS be a unique increasing subsequence with the longest length.

a) True

b) False

### View Answer

5. The number of increasing subsequences with the longest length for the given sequence are:

{10, 9, 8, 7, 6, 5}

a) 3

b) 4

c) 5

d) 6

### View Answer

6. In the brute force implementation to find the longest increasing subsequence, all the subsequences of a given sequence are found. All the increasing subsequences are then selected and the length of the longest subsequence is found. What is the time complexity of this brute force implementation?

a) O(n)

b) O(n^{2})

c) O(n!)

d) O(2^{n})

### View Answer

^{n}, where ‘n’ is the number of elements in the sequence. So, the time complexity is O(2

^{n}).

7. Complete the following dynamic programming implementation of the longest increasing subsequence problem:

#include<stdio.h> int longest_inc_sub(int *arr, int len) { int i, j, tmp_max; int LIS[len]; // array to store the lengths of the longest increasing subsequence LIS[0]=1; for(i = 1; i < len; i++) { tmp_max = 0; for(j = 0; j < i; j++) { if(arr[j] < arr[i]) { if(LIS[j] > tmp_max) ___________; } } LIS[i] = tmp_max + 1; } int max = LIS[0]; for(i = 0; i < len; i++) if(LIS[i] > max) max = LIS[i]; return max; } int main() { int arr[] = {10,22,9,33,21,50,41,60,80}, len = 9; int ans = longest_inc_sub(arr, len); printf("%d",ans); return 0; }

a) tmp_max = LIS[j].

b) LIS[i] = LIS[j].

c) LIS[j] = tmp_max

d) tmp_max = LIS[i].

### View Answer

8. What is the time complexity of the ABOVE dynamic programming implementation used to find the length of the longest increasing subsequence?

a) O(1)

b) O(n)

c) O(n^{2})

d) O(nlogn)

### View Answer

^{2}).

9. What is the space complexity of the ABOVE dynamic programming implementation used to find the length of the longest increasing subsequence?

a) O(1)

b) O(n)

c) O(n^{2})

d) O(nlogn)

### View Answer

10. What is the output of the following program?

#include<stdio.h> int longest_inc_sub(int *arr, int len) { int i, j, tmp_max; int LIS[len]; // array to store the lengths of the longest increasing subsequence LIS[0]=1; for(i = 1; i < len; i++) { tmp_max = 0; for(j = 0; j < i; j++) { if(arr[j] < arr[i]) { if(LIS[j] > tmp_max) tmp_max = LIS[j]; } } LIS[i] = tmp_max + 1; } int max = LIS[0]; for(i = 0; i < len; i++) if(LIS[i] > max) max = LIS[i]; return max; } int main() { int arr[] = {10,22,9,33,21,50,41,60,80}, len = 9; int ans = longest_inc_sub(arr, len); printf("%d",ans); return 0; }

a) 3

b) 4

c) 5

d) 6

### View Answer

11. What is the value stored in LIS[5] after the following program is executed?

#include<stdio.h> int longest_inc_sub(int *arr, int len) { int i, j, tmp_max; int LIS[len]; // array to store the lengths of the longest increasing subsequence LIS[0]=1; for(i = 1; i < len; i++) { tmp_max = 0; for(j = 0; j < i; j++) { if(arr[j] < arr[i]) { if(LIS[j] > tmp_max) tmp_max = LIS[j]; } } LIS[i] = tmp_max + 1; } int max = LIS[0]; for(i = 0; i < len; i++) if(LIS[i] > max) max = LIS[i]; return max; } int main() { int arr[] = {10,22,9,33,21,50,41,60,80}, len = 9; int ans = longest_inc_sub(arr, len); printf("%d",ans); return 0; }

a) 2

b) 3

c) 4

d) 5