ie for i from 1 to n/2
Now compare mind*mid to n
if mid*mid
else
recursively check left half
#include
int sqrtof_perfect(int start,int end,int n)
{
if(start>end)
return 0;
while(start<=end)
{
int mid=(start+end)/2;
//int temp=mid*mid;
if(mid*mid==n)
return mid;
else if(mid*mid>n)
end=mid-1;
else
start=mid+1;
}
return -1;
}
int main()
{
int n=144;//9;//25; // so
printf("\n %d",sqrtof_perfect(1,n/2,n));
getchar();
return 0;
}
TC O(logn)
Sc O(1)
No comments :
Post a Comment