Miscellaneous On Data Structures - Study Mode
[#996] What will be the recurrence relation of the following code? Int sum(int n)
{
If(n==1)
return 1
else
return n+sum(n-1)
}
Correct Answer
(C) T(n) = T(n-1) + O(1)
[#997] Consider the following code: #include<stdio.h>
int recursive_sum(int n)
{
if(n == 0)
return 0
return ________
}
int main()
{
int n = 5
int ans = recursive_sum(n)
printf("%d",ans)
return 0
} Which of the following lines is the recurrence relation for the above code?
Correct Answer
(C) n + recursive_sum(n - 1)
[#998] What will be the output of the following code? int cnt=0
void my_recursive_function(int n)
{
if(n == 0)
return
cnt++
my_recursive_function(n/10)
}
int main()
{
my_recursive_function(123456789)
printf("%d",cnt)
return 0
}
Correct Answer
(D) 9
[#999] Consider the following iterative code snippet to find the largest element: int get_max_element(int *arr,int n)
{
int i, max_element = arr[0]
for(i = 1
i < n
i++)
if(________)
max_element = arr[i]
return max_element
} Which of the following lines should be inserted to complete the above code?
Correct Answer
(A) arr[i] > max_element
[#1000] Consider the following code: #include<stdio.h>
int recursive_sum(int n)
{
if(n == 0)
return 0
return n + recursive_sum(n - 1)
}
int main()
{
int n = 5
int ans = recursive_sum(n)
printf("%d",ans)
return 0
} Which of the following is the base case for the above recursive code?
Correct Answer
(A) if(n == 0)