# YOU CAN DOWNLOAD 200+ SUBJECTS PDF BOOK FOR COMPETITIVE EXAMINATIONS

## Data Structure Questions and Answers-Search an Element in an Array using Recursion

Question 6 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER] |

Consider the following recursive implementation of linear search:

#include<stdio.h> int recursive....search....num(int *arr, int num, int idx, int len) { if(idx == len) return -1; if(arr[idx] == num) return idx; return .....; } int main() { int arr[5] ={1, 3, 3, 3, 5}, num=2, len = 5; int indx = recursive....search....num(arr, num, 0, len); printf("Index of %d is %d", num, indx); return 0; }

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

recursive....search....num(arr, num+1, id, x len); | |

recursive....search....num(arr, num, id, x len); | |

recursive....search....num(arr, num, idx+1, len); | |

recursive....search....num(arr, num+1, idx+1, len); |

Question 6 Explanation:

The recursive call "recursive....search....num(arr, num, idx+1, len)" should be added to complete the above code.

Question 7 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER] |

What is the output of the following code?

#include<stdio.h> int recursive....search....num(int *arr, int num, int idx, int len) { if(idx == len) return -1; if(arr[idx] == num) return idx; return recursive....search....num(arr, num, idx+1, len); } int main() { int arr[8] ={1, 2, 3, 3, 3, 5, 6, 7}, num=5, len = 8; int indx = recursive....search....num(arr, num, 0, len); printf("Index of %d is %d", num, indx); return 0; }

Index of 5 is 5 | |

Index of 5 is 6 | |

Index of 5 is 7 | |

Index of 5 is 8 |

Question 7 Explanation:

The program prints the index of 5, which is 5.

Question 8 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER] |

How many times is the function recursive....search....num() called when the following code is executed?

#include<stdio.h> int recursive....search....num(int *arr, int num, int idx, int len) { if(idx == len) return -1; if(arr[idx] == num) return idx; return recursive....search....num(arr, num, idx+1, len); } int main() { int arr[8] ={1, 2, 3, 3, 3, 5, 6, 7}, num=5, len = 8; int indx = recursive....search....num(arr, num, 0, len); printf("Index of %d is %d", num, indx); return 0; }

5 | |

6 | |

7 | |

8 |

Question 8 Explanation:

The function recursive....search....num() is called 6 times when the above code is executed.

Question 9 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER] |

What is the time complexity of the above recursive implementation of linear search?

O(1) | |

O(n) | |

O(n ^{2}) | |

O(n ^{3}) |

Question 9 Explanation:

The time complexity of the above recursive implementation of linear search is O(n).

There are 9 questions to complete.