Here is the requirement of a problem: Implement int sqrt(int x). Compute and return the truncated square root of x. x is guaranteed to be a non- negative integer. Example 1: Input: 4 Output: 2 Example 2: Input: 8 Output: 2 Explanation: The square root of 8 is 2.82842.., and since we want to return an integer, the decimal part will be truncated. Here is the solution of one programmer: class Solution { public int mysqrt(int x) { if (x == 0) return 0; int left = 1, right = x, ans=right; while (left <= right) { int mid = left + (right - left) / 2; if (mid >= x / mid) { right = mid - 1; ans = mid; } else { left = mid + 1; } } return ans; } } Do you think the above solution is correct? If not, why?

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter8: Arrays And Strings
Section: Chapter Questions
Problem 24PE
icon
Related questions
Question

Please answer the question in the screenshot. Please give reasoning. 

Here is the requirement of a problem:
Implement int sqrt(int x). Compute and return the truncated square root of x. x is guaranteed to be a non-
negative integer.
Example 1:
Input: 4
Output: 2
Example 2:
Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since we want to return an integer, the decimal part
will be truncated.
Here is the solution of one programmer:
class Solution {
public int mysqrt(int x) {
if (x == 0) return 0;
int left = 1, right = x, ans=right;
while (left <= right) {
int mid = left + (right - left) / 2;
if (mid >= x / mid) {
right = mid - 1;
ans = mid;
} else {
left = mid + 1;
}
}
return ans;
}
}
Do you think the above solution is correct? If not, why?
Transcribed Image Text:Here is the requirement of a problem: Implement int sqrt(int x). Compute and return the truncated square root of x. x is guaranteed to be a non- negative integer. Example 1: Input: 4 Output: 2 Example 2: Input: 8 Output: 2 Explanation: The square root of 8 is 2.82842..., and since we want to return an integer, the decimal part will be truncated. Here is the solution of one programmer: class Solution { public int mysqrt(int x) { if (x == 0) return 0; int left = 1, right = x, ans=right; while (left <= right) { int mid = left + (right - left) / 2; if (mid >= x / mid) { right = mid - 1; ans = mid; } else { left = mid + 1; } } return ans; } } Do you think the above solution is correct? If not, why?
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Accessibility feature of Form
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning