Skip to content

Instantly share code, notes, and snippets.

@KeshariPiyush24
Last active September 21, 2024 05:36
Show Gist options
  • Save KeshariPiyush24/dd73d8befa6f444ea731510a58aa0252 to your computer and use it in GitHub Desktop.
Save KeshariPiyush24/dd73d8befa6f444ea731510a58aa0252 to your computer and use it in GitHub Desktop.
367. Valid Perfect Square

Question: 367. Valid Perfect Square

Intution:

Time Complexity: $$O(log(n))$$

Space Complexity: $$O(1)$$

Solution:

class Solution {
    public boolean isPerfectSquare(int num) {
        long low = 1;
        long high = num;
        while (low <= high) {
            long mid = low + (high - low) / 2;
            if (mid * mid == num) return true;
            else if (mid * mid < num) low = mid + 1;
            else high = mid - 1;
        }
        return high * high == num;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment