Repeatedly search an element by doubling it after every successful search | C # 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
using System ; public class GFG { static int findElement ( int [ ] a , int n , int b ) { Array . Sort ( a ) ; int max = a [ n - 1 ] ; while ( b < max ) { if ( Array . BinarySearch ( a , b ) > - 1 ) b *= 2 ; else return b ; } return b ; } public static void Main ( ) { int [ ] a = { 1 , 2 , 3 } ; int n = a . Length ; int b = 1 ; Console . WriteLine ( findElement ( a , n , b ) ) ; } }