


If the ARR is [4,6,9,11,1,2,2] and ‘VAL’ is 4, the answer will be ‘YES’ as 4 exist in ‘ARR’.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains two integers, ‘N’ denoting the number of elements in array ‘ARR’ and ‘VAL’ representing the number to be found.
The following line contains ‘N’ values corresponding to elements of ‘ARR’.
For each test case, print ‘YES’ or ‘NO’ whether the ‘VAL’ exists in the array or not.
Print the output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^7.
1 <=ARR[i] <=10^6.
1 <= VAL <= 10^6.
Time limit: 1 sec
In this approach, we will simply perform a linear search over the array and try to find ‘VAL’. If we find ‘VAL’ , we will return True otherwise, return False.
In this approach, we will use a modified binary search over the array. In every step, we will divide the array into two parts. And will decide which part is sorted and move to the half in which ‘VAL’ may lie.
Conditions will be:
1. If ARR[l] <= ARR[mid],the left half is sorted.
i) If ARR[l]<= ‘VAL’ and ARR[mid]>’VAL’. ‘VAL’ may lie in this range. Move to the left half.
ii) Else, Move to the right half.
2. If ARR[mid] <= ARR[r],the right half is sorted.
i) If ARR[mid] <= ‘VAL’ and ARR[r] > ’VAL’. ‘VAL’ may lie in this range. Move to the right half.
ii) Else, Move to the left half.
But, as the array may contain duplicate elements, these conditions do not guarantee that the respective half is sorted.
For example, if the ARR is [3 1 2 3 3 3 3] and mid=3,l=0 and r=6.The condition ARR[l]<=ARR[mid] is satisfied but the left half is not sorted.
So, we have to deal with the duplicate numbers separately as the following:
1. If A[l] == A[mid],increment l to l+1.
2. If A[r] == A[mid],decrement r to r-1.
So, according to all these conditions, we will make a binary search approach, which will return whether ‘VAL’ is present in ‘ARR’ or not.