Minimum size binary string required such that probability of deleting two 1 's at random is 1/X | Java implementation of the above approach ; Function returns the minimum size of the String ; From formula ; Left limit of r ; Right limit of r ; Smallest integer in the valid range ; Driver Code
import java . util . * ; class GFG { static int MinimumString ( int x ) { int b = 1 ; double left_lim = Math . sqrt ( x ) + 1.0 ; double right_lim = Math . sqrt ( x ) + 2.0 ; int r = 0 ; for ( int i = ( int ) left_lim ; i <= right_lim ; i ++ ) { if ( i > left_lim && i < right_lim ) { r = i ; break ; } } return b + r ; } public static void main ( String [ ] args ) { int X = 2 ; System . out . print ( MinimumString ( X ) ) ; } }