Count Squares Geeks For Geeks
TIME O(sqrt(N)) [GFG time 0.4/1.4]
SPACE O(1)
Here we will be traversing till the square root of number and see weather i*i is more than or equal to n.
class Solution {
static int countSquares(int n) {
int a=0;
int count=0;
for(int i=1;i<Math.sqrt(n);i++){
a=i*i;
if(a<n){
count++;
}
}
//System.out.println(count);
return count;
// code here
}
};
Thanks for Reading.
thanks
ReplyDeletePleasure is mine.😊
Delete