📓
Algorithms
  • Introduction to Data Structures & Algorithms with Leetcode
  • Strings
    • Dutch Flags Problem
      • List Partitoning
    • Counters
      • Majority Vote
      • Removing Parentheses
      • Remove Duplicates from Sorted Array
    • Maths
      • Lone Integer
      • Pigeonhole
      • Check If N and Its Double Exist
      • Find Numbers with Even Number of Digits
    • Two Pointers
      • Remove Element
      • Replace Elements with Greatest Element on Right Side
      • Valid Mountain Array
      • Sort Array by Parity
      • Squares of a Sorted Array
      • Max Consecutive Ones
    • Sliding Window
      • Max Consecutive Ones 3
    • Stacks
      • Balanced Brackets
    • General Strings & Arrays
      • Move Zeros
      • Unique Elements
      • Merge Sorted Array
    • Matrices
      • Valid Square
      • Matrix Search Sequel
  • Trees
    • Untitled
  • Recursion
    • Introduction
    • Backtracking
      • Permutations
  • Dynamic Programming
    • Introduction
    • Minimum (Maximum) Path to Reach a Target
      • Min Cost Climbing Stairs
      • Coin Change
      • Minimum Path Sum
      • Triangle
      • Minimum Cost to Move Chips to The Same Position
      • Consecutive Characters
      • Perfect Squares
    • Distinct Ways
      • Climbing Stairs
      • Unique Paths
      • Number of Dice Rolls with Target Sum
    • Merging Intervals
      • Minimum Cost Tree From Leaf Values
    • DP on Strings
      • Levenshtein Distance
      • Longest Common Subsequence
  • Binary Search
    • Introduction
      • First Bad Version
      • Sqrt(x)
      • Search Insert Position
    • Advanced
      • KoKo Eating Banana
      • Capacity to Ship Packages within D Days
      • Minimum Number of Days to Make m Bouquets
      • Split array largest sum
      • Minimum Number of Days to Make m Bouquets
      • Koko Eating Bananas
      • Find K-th Smallest Pair Distance
      • Ugly Number 3
      • Find the Smallest Divisor Given a Threshold
      • Kth smallest number in multiplication table
  • Graphs
    • Binary Trees
      • Merging Binary Trees
      • Binary Tree Preorder Traversal
      • Binary Tree Postorder Traversal
      • Binary Tree Level Order Traversal
      • Binary Tree Inorder Traversal
      • Symmetric Tree
      • Populating Next Right Pointers in Each Node
      • Populating Next Right Pointers in Each Node II
      • 106. Construct Binary Tree from Inorder and Postorder Traversal
      • Serialise and Deserialise a Linked List
      • Maximum Depth of Binary Tree
      • Lowest Common Ancestor of a Binary Tree
    • n-ary Trees
      • Untitled
      • Minimum Height Trees
    • Binary Search Trees
      • Counting Maximal Value Roots in Binary Tree
      • Count BST nodes in a range
      • Invert a Binary Tree
      • Maximum Difference Between Node and Ancestor
      • Binary Tree Tilt
  • Practice
  • Linked Lists
    • What is a Linked List?
    • Add Two Numbers
      • Add Two Numbers 2
    • Reverse a Linked List
    • Tortoise & Hare Algorithm
      • Middle of the Linked List
  • Bitshifting
    • Introduction
  • Not Done Yet
    • Uncompleted
    • Minimum Cost For Tickets
    • Minimum Falling Path Sum
Powered by GitBook
On this page

Was this helpful?

  1. Binary Search
  2. Introduction

Sqrt(x)

PreviousFirst Bad VersionNextSearch Insert Position

Last updated 4 years ago

Was this helpful?

Implement int sqrt(int x).

Compute and return the square root of x, where x is guaranteed to be a non-negative integer.

Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.

Input: 4
Output: 2

We usually look for the minimal value that satisfies the condition, but in this problem we are looking for the maximal value instead.

The maximal value satisfying condition(k) is False is equal to condition(k) is True minus 1.

This is why we need to decide which value to return, left or left -1.

Let's look at this in more detail.

Our condition is:

def condition(mid):
    return mid * mid <= x

Given the number 4, we have this array:

[0, 1, 2, 3]

This means our array looks like:

0 * 0 <= x: True
1 * 1 <= x: True
2 * 2 <= x: True
3 * 3 <= x: False

[True, True, True, False]

So our search needs to find the maximal value that does not equate to False. To do that. Let's look at what happens to our left and right pointers as we move along.

Our pointers are:

  • left = 0

  • right = 4

We now begin looping.

  • Mid = 2

0 * 0 <= x, so we change our left pointer to mid + 1, which is 2 + 1.

  • Left = 3

  • right = 4

  • Mid = 3 + (4 - 3) // 2 = 3

Now we calculate for mid again. 3*3 is not <= 4, so we change our right pointer to mid.

  • left = 3

  • right = 3

  • mid = 3

We exit out loop because 3 is not valid. Because 3 is not valid, we return the element right before 3 which is 2.

Therefore our answer is 2.

If you recall our normal binary search:

    left, right = 0, len(array)
    while left < right:
        mid = left + (right - left) // 2
        if condition(mid):
            right = mid
        else:
            left = mid + 1
    return left

We've switched around right and mid. Let's see what'd happen if we went with this.

This is because we usually look for the minimal value that satisfies the condition (thus we want to use the right to calculate this), but in this case we are looking for the maximal value instead (so we want to use left). This is because the array is sorted. Moving right implies that we are looking for the minimal value, as the smallest values come first.

Because the array is sorted in increasing order, the largest value is always last. So we use left to find that value

And now our code looks like:

def mySqrt(x: int) -> int:
    if x <= 1:
        return x
    
    def condition(mid):
        return mid * mid <= x
    
    left, right = 0, x
    while left < right:
        mid = left + (right - left) // 2
        if condition(mid):
            left = mid + 1
        else:
            right = mid
    return left - 1

https://leetcode.com/problems/sqrtx/