Check if an element is present in an array using at most floor(N / 2) + 2 comparisons
Last Updated :
11 Oct, 2021
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check whether X
// is present in the array A[]
void findElement(int A[], int N, int X)
{
// Initialise a pointer
int i = 0;
// Store the number
// of comparisons
int Comparisons = 0;
// Variable to store product
int T = 1;
string Found = "No";
// Check is N is odd
Comparisons++;
if (N % 2 == 1) {
// Update i and T
i = 1;
T *= (A[0] - X);
}
// Traverse the array
for (; i < N; i += 2) {
// Check if i < N
Comparisons += 1;
// Update T
T *= (A[i] - X);
T *= (A[i + 1] - X);
}
// Check if T is equal to 0
Comparisons += 1;
if (T == 0) {
cout << "Yes " << Comparisons;
}
else {
cout << "No";
}
}
// Driver Code
int main()
{
// Given Input
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;
// Function Call
findElement(A, N, X);
return 0;
}
Java
// Java program for the above approach
public class GFG {
// Function to check whether X
// is present in the array A[]
static void findElement(int[] A, int N, int X)
{
// Initialise a pointer
int i = 0;
// Store the number
// of comparisons
int Comparisons = 0;
// Variable to store product
int T = 1;
// Check is N is odd
Comparisons++;
if (N % 2 == 1) {
// Update i and T
i = 1;
T *= (A[0] - X);
}
// Traverse the array
for (; i < N; i += 2) {
// Check if i < N
Comparisons += 1;
// Update T
T *= (A[i] - X);
T *= (A[i + 1] - X);
}
// Check if T is equal to 0
Comparisons += 1;
if (T == 0) {
System.out.println("Yes " + Comparisons);
}
else {
System.out.println("No");
}
}
// Driver code
public static void main(String[] args)
{
// Given Input
// Given Input
int[] A = { -3, 5, 11, 3, 100, 2, 88,
22, 7, 900, 23, 4, 1 };
int N = A.length;
int X = 1;
// Function Call
findElement(A, N, X);
}
}
// This code is contributed by abhinavjain194
Python3
# Python 3 program for the above approach
# Function to check whether X
# is present in the array A[]
def findElement(A, N, X):
# Initialise a pointer
i = 0
# Store the number
# of comparisons
Comparisons = 0
# Variable to store product
T = 1
Found = "No"
# Check is N is odd
Comparisons += 1
if (N % 2 == 1):
# Update i and T
i = 1
T *= (A[0] - X)
# Traverse the array
while(i< N):
# Check if i < N
Comparisons += 1
# Update T
T *= (A[i] - X)
T *= (A[i + 1] - X)
i += 2
# Check if T is equal to 0
Comparisons += 1
if (T == 0):
print("Yes",Comparisons)
else:
print("No")
# Driver Code
if __name__ == '__main__':
# Given Input
A = [-3, 5, 11, 3, 100, 2, 88, 22, 7, 900, 23, 4, 1]
N = len(A)
X = 1
# Function Call
findElement(A, N, X)
# This code is contributed by bgangwar59.
C#
// C# program for the above approach
using System;
class GFG{
// Function to check whether X
// is present in the array A[]
static void findElement(int[] A, int N, int X)
{
// Initialise a pointer
int i = 0;
// Store the number
// of comparisons
int Comparisons = 0;
// Variable to store product
int T = 1;
// Check is N is odd
Comparisons++;
if (N % 2 == 1)
{
// Update i and T
i = 1;
T *= (A[0] - X);
}
// Traverse the array
for(; i < N; i += 2)
{
// Check if i < N
Comparisons += 1;
// Update T
T *= (A[i] - X);
T *= (A[i + 1] - X);
}
// Check if T is equal to 0
Comparions += 1;
if (T == 0)
{
Console.Write("Yes " + Comparisons);
}
else
{
Console.Write("No");
}
}
// Driver Code
public static void Main()
{
// Given Input
int[] A = { -3, 5, 11, 3, 100, 2, 88,
22, 7, 900, 23, 4, 1 };
int N = A.Length;
int X = 1;
// Function Call
findElement(A, N, X);
}
}
// This code is contributed by ukasp
JavaScript
<script>
// JavaScript program for the above approach
// Function to check whether X
// is present in the array A[]
function findElement(A, N, X)
{
// Initialise a pointer
var i = 0;
// Store the number
// of comparisons
var Comparisons = 0;
// Variable to store product
var T = 1;
var Found = "No";
// Check is N is odd
Comparisons += 1;
if (N % 2 == 1) {
// Update i and T
i = 1;
T *= (A[0] - X);
}
// Traverse the array
for (; i < N; i += 2) {
// Check if i < N
Comparisons += 1;
// Update T
T *= (A[i] - X);
T *= (A[i + 1] - X);
}
// Check if T is equal to 0
Comparisons += 1;
if (T == 0) {
document.write("Yes " + Comparisons);
}
else {
document.write("No");
}
}
// Driver Code
// Given Input
var A = [-3, 5, 11, 3, 100, 2, 88,
22, 7, 900, 23, 4, 1];
var N = A.length;
var X = 1;
// Function Call
findElement(A, N, X);
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
C++ Program to Check for Majority Element in a sorted array Question: Write a function to find if a given integer x appears more than n/2 times in a sorted array of n integers. Basically, we need to write a function say isMajority() that takes an array (arr[] ), arrayâs size (n) and a number to be searched (x) as parameters and returns true if x is a majorit
6 min read
Select maximum types from an array with limit of n/2 Given an array arr[] of n items, with n being an even number and arr[i] representing the type of item. The task is to find the maximum number of different types of items a person can select with a limit on total selected items as n/2.Examples:Input: arr[] = {2, 2, 3, 1}Output: 2Explanation: Person c
4 min read
Print all array elements appearing more than N / K times Given an array arr[] of size N and an integer K, the task is to find all the array elements that appear more than (N / K) times. Examples: Input: arr[] = { 1, 2, 6, 6, 6, 6, 6, 10 }, K = 4Output: 6Explanation: The frequency of 6 in the array is greater than N / K(= 2). Therefore, the required output
15+ min read
Search an element in an unsorted array using minimum number of comparisons Given an array of n distinct integers and an element x. Search the element x in the array using minimum number of comparisons. Any sort of comparison will contribute 1 to the count of comparisons. For example, the condition used to terminate a loop, will also contribute 1 to the count of comparisons
7 min read
Check if an array contains all elements of a given range An array containing positive elements is given. 'A' and 'B' are two numbers defining a range. Write a function to check if the array contains all elements in the given range. Examples : Input : arr[] = {1 4 5 2 7 8 3} A : 2, B : 5Output : YesInput : arr[] = {1 4 5 2 7 8 3} A : 2, B : 6Output : NoRec
15+ min read