Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Is Perfect Square Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Is Perfect Square | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given a positive integer num, write a function that returns true if num is a perfect square else return false.

Naive Approach

A simple way to solve this problem is to run a loop from i = 1 to i = n and as soon as i * i is greater than equal to n break the loop. If i * i is equal to n at some point return true otherwise return false. 

Analysis

  • Time Complexity: O(sqrt(n))
  • Space Complexity: O(1)

Implementation

C++
bool isPerfectSquare(int num) {
   for (int i = 1; i <= num; i++) {
       if (i * i == num) {
           return true;
       } else if (i * i > num) {
           return false;
       }
   }
}
Java
class Solution {
   boolean isPerfectSquare (int num) {
       for (int i = 1; i <= num; i++) {
           if (i * i == num) {
               return true;
           } else if (i * i > num) {
               return false;
           }
       }
       return false;
   }
}

Optimal Approach

The problem can be solved by using the concept of binary search from 1 to n to find a value whose square is equal to n.

  • Declare three variables low=1(lower bound) and high=n(upper bound), mid.
  • Perform a binary search until (low<=high).
  • Initialize mid=(low+high)/2 and check if the square of mid is equal to n, return true. Otherwise, if the square of mid is less than n, search in the second half else search in the first half.
  • Return false as the default condition.

Analysis

  • Time Complexity: O(log(n))
  • Space Complexity: O(1)

Implementation

C++
bool searchRoot(int low, int high, int num) {
   if (high >= low) {
       int mid = (high + low) / 2;
       int square = mid * mid;
       if (square == num) {
           return true;
       } else if (square > num) {
           return searchRoot(low, mid - 1, num);
       } else {
           return searchRoot(mid + 1, high, num);
       }
   }
   return false;
}
bool isPerfectSquare(int num) {
   int maxVal = 10000; // To avoid overflow while computing square
   return searchRoot(1, maxVal, num);
}
Java
class Solution {
   boolean searchRoot(int low, int high, int num) {
       if (high >= low) {
           int mid = (high + low) / 2;
           int square = mid * mid;
           if (square == num) {
               return true;
           } else if (square > num) {
               return searchRoot(low, mid - 1, num);
           } else {
               return searchRoot(mid + 1, high, num);
           }
       }
       return false;
   }
   boolean isPerfectSquare (int num) {
       int maxVal = 10000;
       return searchRoot(1, maxVal, num);
   }
}
Related Content
Contains Element?
Insert Position in Sorted Array
Matrix Search
Median of Row-wise Sorted Matrix
Negative numbers in sorted array
Next Greater Element In Sorted Array
Non-Repeating Element
Search Range
Search Rotated Sorted Array
Square Root
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet