-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathCountSquares.java
More file actions
31 lines (30 loc) · 773 Bytes
/
CountSquares.java
File metadata and controls
31 lines (30 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution
{
// return either a perfect square root or floor value
public static int mySqrt(int N ) {
int start=1;
int end=N;
while(start<=end)
{
int mid = start+(end-start)/2;
if(mid<=N/mid)
{
if(N%mid==0 && mid==N/mid)
{
return mid;
}
start=mid+1;
}
else end=mid-1;
}
return start;
}
static int countSquares(int N) {
// code here
int sqrRoot = mySqrt(N);
return sqrRoot-1;
}
}
// 1. find the square root
// 2. if N is a perfect square then return squareroot -1
// 3. if N is not a perfect sqaure then return ceil(sqaureroot)-1