Check if an element is present in an array using at most floor ( N / 2 ) + 2 comparisons | Function to check whether X is present in the array A [ ] ; Initialise a pointer ; Store the number of comparisons ; Variable to store product ; Check is N is odd ; Update i and T ; Traverse the array ; Check if i < N ; Update T ; Check if T is equal to 0 ; Driver Code ; Given Input ; Function Call
def findElement ( A , N , X ) : NEW_LINE INDENT i = 0 NEW_LINE Comparisons = 0 NEW_LINE T = 1 NEW_LINE Found = " No " NEW_LINE Comparisons += 1 NEW_LINE if ( N % 2 == 1 ) : NEW_LINE INDENT i = 1 NEW_LINE T *= ( A [ 0 ] - X ) NEW_LINE DEDENT while ( i < N ) : NEW_LINE INDENT Comparisons += 1 NEW_LINE T *= ( A [ i ] - X ) NEW_LINE T *= ( A [ i + 1 ] - X ) NEW_LINE i += 2 NEW_LINE DEDENT Comparisons += 1 NEW_LINE if ( T == 0 ) : NEW_LINE INDENT print ( " Yes " , Comparisons ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ - 3 , 5 , 11 , 3 , 100 , 2 , 88 , 22 , 7 , 900 , 23 , 4 , 1 ] NEW_LINE N = len ( A ) NEW_LINE X = 1 NEW_LINE findElement ( A , N , X ) NEW_LINE DEDENT