Skip to main content

Command Palette

Search for a command to run...

Valid Perfect Square

Published
1 min read
Valid Perfect Square
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

Given a positive integer num, return true if num is a perfect square or false otherwise.

A perfect square is an integer that is the square of an integer. In other words, it is the product of some integer with itself.

You must not use any built-in library function, such as sqrt.

LeetCode Problem - 267: Link | Click Here

class Solution {
    // Method to check if a number is a perfect square
    public boolean isPerfectSquare(int num) {
        // Initialize the search range
        long start = 1;
        long end = num;

        // Binary search loop to find the square root of the given number
        while (start <= end) {
            // Calculate the middle value of the current search range
            long mid = start + (end - start) / 2;
            long midProd = mid * mid;

            // Check if the square of the middle value equals the given number
            if (midProd == num) {
                return true;  // Found a perfect square
            } else if (midProd > num) {
                end = (mid - 1);  // Reduce the search range
            } else {
                start = (mid + 1);  // Increase the search range
            }
        }

        // No perfect square found within the given range
        return false;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.