Given an array arr = {35,40, 45,77,89,90,94,99,100} and key = 99; what are the mid values(corresponding array elements) in the first and second levels of recursion? Options 90 and 99 90 and 94 89 and 94 91 and 94
Question
Given an array arr = {35,40, 45,77,89,90,94,99,100} and key = 99; what are the mid values(corresponding array elements) in the first and second levels of recursion?
Options 90 and 99
90 and 94
89 and 94
91 and 94
Solution
The question is about Binary Search, which is a divide and conquer algorithm. In this algorithm, we first find the middle element of array. If the key is present at the middle, we return the mid index. Else if the key is greater than the mid element, then key can only lie in the right (greater) half. So, we apply the algorithm again for the right half. Otherwise, the key is smaller, the key must lie in the left (lower) half. So, we apply the algorithm for the left half.
Given array is arr = {35,40, 45,77,89,90,94,99,100} and key = 99.
The array is of size 9. So, the mid element in the first level of recursion is at index mid = (0+8)/2 = 4. So, the mid value is arr[4] = 89.
In the second level of recursion, we consider the right half of the array because the key is greater than the mid value. The right half of the array is {90,94,99,100}. The mid element in this array is at index mid = (0+3)/2 = 1 (considering 0 based indexing). So, the mid value is arr[1] = 94.
So, the mid values in the first and second levels of recursion are 89 and 94. Therefore, the correct option is "89 and 94".
Similar Questions
Given an array arr = {45,77,89,90,94,99,100} and key = 100; What are the mid values(corresponding array elements) generated in the first and second iterations?Options90 and 9989 and 9494 and 9990 and 100
given an array a[] = {4,6,7,8,11} and key =11, what is the level of recursion?( assume binary search)5432
Multi Choice Type QuestionWhat are the mid values (corresponding array items) produced in the first and second iterations for an array arr = [12, 24, 36, 48, 60, 72, 84] and key = 84?Marks : 1Negative Marks : 0Answer here36 and 6024 and 3660 and 7248 and 72
Given the array declaration int arr[5] = {1, 2, 3};, what are the values of arr[3] and arr[4]?Both arr[3] and arr[4] are 0Both arr[3] and arr[4] are uninitializedarr[3] is 3 and arr[4] is 3arr[3] is 2 and arr[4] is 2
If you declare an array int arr[10];, what is the range of valid indexes for accessing elements of the array?0 to 101 to 100 to 91 to 9
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.