Mary is working to find the square root of a number in C++ language using a binary search algorithm. Which of the following options can she perform to get an optimized solution?Constraints0<=x<=2^31-1Optionsint fun(int x) { if(x==1) return x; int low=0,high=x; int res=0; while(low<high){ int mid=(low+high)/2; if(mid>x/mid) high=mid; else{ res=mid; low=mid+1; } } return res;}int fun(int x) { if(x==1) return x; int low=0,high=x; int res=0; while(low<=high){ int mid=low+(high-low)/2; if(mid>x/(mid*mid) high=mid; else{ res=mid; low=mid+1; } } return res;}int fun(int x) { if(x==1) return x; int low=0,high=x; int res=0; while(low<high){ int mid=low+(high-low)/2; else if((mid*mid*mid)>x) high=mid; else{ res=mid; low=mid+1; } } return res;}int fun(int x) { int res=0; for(int i=0;i<=x;i++){ if((i*i)<=x) res=i; } return res;}
Question
Mary is working to find the square root of a number in C++ language using a binary search algorithm. Which of the following options can she perform to get an optimized solution?Constraints0<=x<=2^31-1Optionsint fun(int x) { if(x==1) return x; int low=0,high=x; int res=0; while(low<high){ int mid=(low+high)/2; if(mid>x/mid) high=mid; else{ res=mid; low=mid+1; } } return res;}int fun(int x) { if(x==1) return x; int low=0,high=x; int res=0; while(low<=high){ int mid=low+(high-low)/2; if(mid>x/(midmid) high=mid; else{ res=mid; low=mid+1; } } return res;}int fun(int x) { if(x==1) return x; int low=0,high=x; int res=0; while(low<high){ int mid=low+(high-low)/2; else if((midmidmid)>x) high=mid; else{ res=mid; low=mid+1; } } return res;}int fun(int x) { int res=0; for(int i=0;i<=x;i++){ if((ii)<=x) res=i; } return res;}
Solution
The second option is the most optimized solution for finding the square root of a number in C++ using a binary search algorithm. Here's why:
int fun(int x) {
if(x==1) return x;
int low=0,high=x;
int res=0;
while(low<=high){
int mid=low+(high-low)/2;
if(mid>x/mid) high=mid;
else{
res=mid;
low=mid+1;
}
}
return res;
}
This function uses binary search to find the square root of a number. The binary search algorithm is efficient because it reduces the search space by half at each step, leading to a time complexity of O(log n).
The function starts by initializing two pointers, low and high, to 0 and x respectively. It then enters a while loop that continues until low is greater than high.
In each iteration of the loop, it calculates the middle value mid as the average of low and high. It then checks if mid is greater than x/mid. If it is, it means that mid is too large to be the square root of x, so it sets high to mid. Otherwise, it sets res to mid and low to mid+1.
Finally, the function returns res, which is the largest integer less than or equal to the square root of x.
The other options are either incorrect or less efficient. The first and third options are incorrect because they don't correctly calculate the square root. The fourth option is less efficient because it uses a linear search, which has a time complexity of O(n).
Similar Questions
Propose a binary search algorithm, 𝐵𝐵, that decides the perfect square problem.
What is Heron’s algorithm for finding the square root of a number?
Given a sorted array A of size N, along with an integer target K, implement Binary Search to find the target K in the given array.Input FormatThe first line of input contains an integer N - the size of an array and target K. The second line contains the elements of the array.Output FormatFor each iteration of Binary Search, print the values of low, high, and mid. At the end, if the target K is found in the array, print "True" otherwise, print "False".Constraints1 <= N <= 201 <= A[i] <= 103ExampleInput 19 121 4 6 7 10 11 12 20 23Output 10 8 45 8 6TrueInput 210 213 5 8 11 15 17 19 23 26 30Output 20 9 45 9 75 6 56 6 6False
Write a C program to find the roots of a Quadratic equation.
Which of the following is the correct way to find the middle element in a Binary Search algorithm implemented in C?mid = (low + high) / 2;mid = low + (high low) / 2;mid = low + (high low) / 3;mid = (low + high) 2;
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.