📌  相关文章
📜  最多使用 floor(N / 2) + 2 次比较检查数组中是否存在元素

📅  最后修改于: 2022-05-13 01:56:05.762000             🧑  作者: Mango

最多使用 floor(N / 2) + 2 次比较检查数组中是否存在元素

给定一个大小为N的数组A[]和一个整数X ,任务是检查X是否存在于A[]中且不超过floor(N/2) + 2 次比较。
注意:对于任何索引i(i < N)(A[i] == X)都被视为单独的比较。

例子:

方法:按照以下步骤解决问题:

  • 初始化一个变量,比如T1 ,以存储所有数组元素的乘积 – X(A[i] – X)
  • 初始化一个变量,比如比较0 ,以存储所需的比较次数。
  • 初始化指针, i0 ,遍历数组。
  • 如果 N 的值为奇数,则将比较加 1,因为检查了 N 的奇偶校验并将T更新为T * (A[0] – X)并将i更新为1
  • 因此, [i, N – 1]N – i范围内的元素个数总是偶数。
  • [i, N-1]范围内遍历数组A[ ]并执行以下步骤:
    • 将 T 的值更新为T * (A[i] – X) * (A[i + 1] – X)
    • 将 i 更新为i + 2并将比较增加1 ,因为条件i < N已检查。
  • 如果T的值为0 ,则将比较增加1 ,因为比较T的相等性。因此, X存在于A[]中并打印“是”比较次数。
  • 否则,打印“否”作为结果。
  • 该算法保证比较次数≤ floor(N / 2) + 2

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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


输出:
Yes 8

时间复杂度: O(N)
辅助空间: O(1)