Check if an element is present in an array using at most floor(N / 2) + 2 comparisons
Given an array A[] of size N and an integer X, the task is to check if X exists in A[] with no more than floor(N/2) + 2 comparisons.
Note: For any index i, (i < N) or (A[i] == X) are considered as separate comparisons.
Examples:
Input: A[] = {-3, 5, 11, 3, 100, 2, 88, 22, 7, 900, 23, 4, 1}, X = 88
Output: Yes 8
Explanation: X = 88 exists in the given array, A[] and is detected with 8 comparisons.
Input: A[]= {-3, 5, 11, 3, 100, 2, 88, 22, 7, 900, 23, 4, 1}, X = 6
Output: No
Explanation: X = 6 doesn’t exist in the given array, A[].
Approach: Follow the steps to solve the problem:
- Initialize a variable, say T as 1, to store product of all array elements – X i.e (A[i] – X)
- Initialize a variable, say comparisons as 0, to store the number of comparisons required.
- Initialize pointer, i as 0 to traverse the array.
- If the value of N is odd, increment comparisons by 1 because parity of N is checked and update T to T * (A[0] – X) and i to 1.
- Therefore, the number of elements in the range [i, N – 1] i.e N – i is always even.
- Traverse the array, A[] in range [i, N-1] and perform the following steps:
- Update the value of T to T * (A[i] – X) * (A[i + 1] – X).
- Update i to i + 2 and increment comparisons by 1 because condition i < N is checked.
- If the value of T is 0, increment comparisons by 1 because the equality of T is compared. Therefore, X exists in A[] and print “Yes” and number of comparisons.
- Otherwise, Print “No” as the result.
- The algorithm guarantees that the number of comparisons ? floor(N / 2) + 2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findElement( int A[], int N, int X)
{
int i = 0;
int Comparisons = 0;
int T = 1;
string Found = "No" ;
Comparisons++;
if (N % 2 == 1) {
i = 1;
T *= (A[0] - X);
}
for (; i < N; i += 2) {
Comparisons += 1;
T *= (A[i] - X);
T *= (A[i + 1] - X);
}
Comparisons += 1;
if (T == 0) {
cout << "Yes " << Comparisons;
}
else {
cout << "No" ;
}
}
int main()
{
int A[] = { -3, 5, 11, 3, 100, 2, 88,
22, 7, 900, 23, 4, 1 };
int N = sizeof (A) / sizeof (A[0]);
int X = 1;
findElement(A, N, X);
return 0;
}
|
Java
public class GFG {
static void findElement( int [] A, int N, int X)
{
int i = 0 ;
int Comparisons = 0 ;
int T = 1 ;
Comparisons++;
if (N % 2 == 1 ) {
i = 1 ;
T *= (A[ 0 ] - X);
}
for (; i < N; i += 2 ) {
Comparisons += 1 ;
T *= (A[i] - X);
T *= (A[i + 1 ] - X);
}
Comparisons += 1 ;
if (T == 0 ) {
System.out.println( "Yes " + Comparisons);
}
else {
System.out.println( "No" );
}
}
public static void main(String[] args)
{
int [] A = { - 3 , 5 , 11 , 3 , 100 , 2 , 88 ,
22 , 7 , 900 , 23 , 4 , 1 };
int N = A.length;
int X = 1 ;
findElement(A, N, X);
}
}
|
Python3
def findElement(A, N, X):
i = 0
Comparisons = 0
T = 1
Found = "No"
Comparisons + = 1
if (N % 2 = = 1 ):
i = 1
T * = (A[ 0 ] - X)
while (i< N):
Comparisons + = 1
T * = (A[i] - X)
T * = (A[i + 1 ] - X)
i + = 2
Comparisons + = 1
if (T = = 0 ):
print ( "Yes" ,Comparisons)
else :
print ( "No" )
if __name__ = = '__main__' :
A = [ - 3 , 5 , 11 , 3 , 100 , 2 , 88 , 22 , 7 , 900 , 23 , 4 , 1 ]
N = len (A)
X = 1
findElement(A, N, X)
|
C#
using System;
class GFG{
static void findElement( int [] A, int N, int X)
{
int i = 0;
int Comparisons = 0;
int T = 1;
Comparisons++;
if (N % 2 == 1)
{
i = 1;
T *= (A[0] - X);
}
for (; i < N; i += 2)
{
Comparisons += 1;
T *= (A[i] - X);
T *= (A[i + 1] - X);
}
Comparions += 1;
if (T == 0)
{
Console.Write( "Yes " + Comparisons);
}
else
{
Console.Write( "No" );
}
}
public static void Main()
{
int [] A = { -3, 5, 11, 3, 100, 2, 88,
22, 7, 900, 23, 4, 1 };
int N = A.Length;
int X = 1;
findElement(A, N, X);
}
}
|
Javascript
<script>
function findElement(A, N, X)
{
var i = 0;
var Comparisons = 0;
var T = 1;
var Found = "No" ;
Comparisons += 1;
if (N % 2 == 1) {
i = 1;
T *= (A[0] - X);
}
for (; i < N; i += 2) {
Comparisons += 1;
T *= (A[i] - X);
T *= (A[i + 1] - X);
}
Comparisons += 1;
if (T == 0) {
document.write( "Yes " + Comparisons);
}
else {
document.write( "No" );
}
}
var A = [-3, 5, 11, 3, 100, 2, 88,
22, 7, 900, 23, 4, 1];
var N = A.length;
var X = 1;
findElement(A, N, X);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
11 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...