4. Count squares
4. Count squares
Problem link :- click here
package program;
public class Searching__4
{
/*
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 is in the sample space S.
n = 9
output = 2
lets find the square-root the given number
and the return 1 number less than the square root of the given number
*/
static int countSquares(int n)
{
int ans = (int) Math.sqrt(n) - 1 ;
return ans;
}
public static void main(String[] args)
{
int ans = countSquares(16);
System.out.println(ans);
}
}
Time complexity :- O(sqrt(n))
Space complexity :- O(1)
Comments
Post a Comment