Count Squares | Practice | GeeksforGeeks

Consider a sample space S consisting of all perfect squares starting from 1, 4, 9 and so on. You are given a number N, you have to output the number of integers less than N in the sample space S.
 
Example 1:
Input :
N = 9
Output:
2
Explanation:
1 and 4 are the only Perfect Squares
less than 9. So, the Output is 2.
Example 2:
Input :
N = 3
Output:
1
Explanation:
1 is the only Perfect Square
less than 3. So, the Output is 1.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function countSquares() which takes an Integer N as input and returns the answer.
 
Expected Time Complexity: O(sqrt(N))
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 108
0
hrithikjain98892 days ago
Total Time Taken:
0.02/1.31
int countSquares(int N) {       // code here     int x=sqrt(N);     if(x*x==N)     return x-1;     else     return x;   }
0
jainamkothari142 days ago
static int countSquares(int N) {              int count=0;       int i=1;       while(i*i<N)       {           count++;           i++;       }       return count;   }
0
dharmikpatel4912 days ago
int countSquares(int N) {       int ans;       float sq = sqrt(N);       if(floor(sq) != sq){           ans = (int)sq;       } else {           ans = (int)sq-1;       }       return ans;   }
0
btech60063192 days ago
       int i;       for(i=0; i*i<N; i++);       return i-1;
0
amitrrrvaa00035 days ago
Python Code
return int(math.sqrt(N-1))
0
satyamjaiswal88281 week ago
int countSquares(int N) {       // code here       return sqrt(N-1);   }
//sqrt is square root function in stl library.
0
rohanmeher1641 week ago
//Simple Java Solution
 
class Solution {   static int countSquares(int N) {     int ans=0;     for(int i=0;i*i<N;i++)     {         //   we dont need to create any 'count' variable because,value of 'i' and 'count' will be same.         ans=i;     }     return ans;   }}
+1
mayank180919991 week ago
  int countSquares(int N) {
        // code here
        int count=0;
        for(int i=1;i<sqrt(N);i++){
            if(i*i<N){
                count++;
            }
        }
     return count;
    }
0
sharmanitish1832 weeks ago
class Solution {   static int countSquares(int N) {       return (int)Math.sqrt(N-1);   }}
0
anandpatel982601 month ago
Easy to understand approach
class Solution { public:   int countSquares(int N) {       int count=0;       for(int i=1;i*i<N;i++)count++;       return count;   }};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.