What is the time complexity of the binary search algorithm?
Which of the following methods is used to find the square root of a number?
Which numerical method is used to find the approximate value of a definite integral?
Which numerical method can be used to solve a system of linear equations?
What is the main advantage of using Ternary Search over Binary Search?
What will be the output of the following code?
#include <iostream>
using namespace std;
int binarySearch(int arr[], int target, int low, int high) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
int main() {
int arr[] = {2, 5, 7, 10, 15};
int target = 7;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, target, 0, n - 1);
cout << "Result: " << result << endl;
return 0;
}
What will be the output of the following code?
#include <iostream>
using namespace std;
double squareRoot(double x) {
double low = 0.0, high = x, mid;
while (high - low > 1e-9) {
mid = low + (high - low) / 2;
if (mid * mid > x) {
high = mid;
} else {
low = mid;
}
}
return low;
}
int main() {
double num = 16.0;
double result = squareRoot(num);
cout << "Result: " << result << endl;
return 0;
}
What will be the output of the following code?
#include <iostream>
using namespace std;
double ternarySearch(double low, double high, double target) {
while (high - low > 1e-9) {
double leftThird = low + (high - low) / 3;
double rightThird = high - (high - low) / 3;
if (f(leftThird) < f(rightThird)) {
low = leftThird;
} else {
high = rightThird;
}
}
return low;
}
double f(double x) {
return x * x * x - 4 * x + 1;
}
int main() {
double result = ternarySearch(-1000, 1000, 0);
cout << "Result: " << result << endl;
return 0;
}
Which of the following problems can be efficiently solved using Binary Search?
Consider an array of integers where each element appears twice except for one element that appears only once. How can you efficiently find the element that appears only once using Binary Search?
Which of the following is a disadvantage of using Newton's Method for finding roots of equations?
Which of the following is an advantage of using Ternary Search over Binary Search for finding the maximum or minimum point in a unimodal function?
What will be the output of the following code?
#include <iostream>
using namespace std;
int binarySearch(int arr[], int target, int low, int high) {
if (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
return binarySearch(arr, target, mid + 1, high);
} else {
return binarySearch(arr, target, low, mid - 1);
}
}
return -1;
}
int main() {
int arr[] = {2, 5, 7, 10, 15};
int target = 7;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, target, 0, n - 1);
cout << "Result: " << result << endl;
return 0;
}
What will be the output of the following code?
#include <iostream>
using namespace std;
double squareRoot(double x) {
double low = 0.0, high = x, mid;
while (high - low > 1e-9) {
mid = low + (high - low) / 2;
if (mid * mid > x) {
high = mid;
} else {
low = mid;
}
}
return low;
}
int main() {
double num = 9.0;
double result = squareRoot(num);
cout << "Result: " << result << endl;
return 0;
}
What will be the output of the following code?
#include <iostream>
using namespace std;
double ternarySearch(double low, double high, double target) {
while (high - low > 1e-9) {
double leftThird = low + (high - low) / 3;
double rightThird = high - (high - low) / 3;
if (f(leftThird) < f(rightThird)) {
low = leftThird;
} else {
high = rightThird;
}
}
return low;
}
double f(double x) {
return x * x * x - 4 * x + 1;
}
int main() {
double result = ternarySearch(-1000, 1000, 1);
cout << "Result: " << result << endl;
return 0;
}