// Efficient Code import java.util.*; import java.io.*; class GFG { static int sqRootFloor(int x) { int low = 1, high = x, ans = -1; while(low <= high) { int mid = (low + high) / 2; int mSq = mid * mid; if(mSq == x) return mid; else if(mSq > x) high = mid - 1; else { low = mid + 1; ans = mid; } } return ans; } public static void main(String args[]) { System.out.println(sqRootFloor(10)); } } // Naive Code import java.util.*; import java.io.*; class GFG { static int sqRootFloor(int x) { int i = 1; while(i * i <= x) i++; return i - 1; } public static void main(String args[]) { System.out.println(sqRootFloor(15)); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter