From 5f8353c1438a69a5df27ac9be2428c2824e5b57c Mon Sep 17 00:00:00 2001 From: Sneha Shukla <72255255+Scarlet-Coder@users.noreply.github.com> Date: Sat, 22 Oct 2022 23:18:56 +0530 Subject: [PATCH] Create 367_Valid_Perfect_square.java Added code to check for perfect square in java --- .../367_Valid_Perfect_square.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Hacktoberfest2022/367_Valid_Perfect_square.java diff --git a/Hacktoberfest2022/367_Valid_Perfect_square.java b/Hacktoberfest2022/367_Valid_Perfect_square.java new file mode 100644 index 0000000..5cbf4a9 --- /dev/null +++ b/Hacktoberfest2022/367_Valid_Perfect_square.java @@ -0,0 +1,20 @@ +class Solution { + public boolean isPerfectSquare(int num) { + if (num == 1) + return true; + int l=1,r=num/2; + + while(l<=r) + { + int mid=l+(r-l)/2; + long sq=mid*(long)mid; + if(sq==num) + return true; + if(sq>num) + r=mid-1; + else + l=mid+1; + } + return false; + } +}