Repeatedly search an element by doubling it after every successful search | Java program to repeatedly search an element by doubling it after every successful search ; Sort the given array so that binary search can be applied on it ; Maximum array element ; search for the element b present or not in array ; Driver code
import java . util . Arrays ; public class Test4 { static int findElement ( int a [ ] , int n , int b ) { Arrays . sort ( a ) ; int max = a [ n - 1 ] ; while ( b < max ) { if ( Arrays . binarySearch ( a , b ) > - 1 ) b *= 2 ; else return b ; } return b ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 2 , 3 } ; int n = a . length ; int b = 1 ; System . out . println ( findElement ( a , n , b ) ) ; } }