Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions cpp/0367-valid-perfect-square.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Approach: Binary search the number such that divind num by that number gives the number itself

Time Complexity: log(n)
Space Complexity: O(1)
*/
class Solution {
public:
bool isPerfectSquare(int num) {
if(num ==1) {return true;}
int l = 0,r = num;
while(l<r){
int m = l + (r-l)/2;
float x = (float)num / (float)m;
if(x==m){
return true;
}
else if (x<m){
r = m;
}
else {
l = m +1;
}
}

return false;
}
};