📜  在排序数组中找到唯一的遗漏号码

📅  最后修改于: 2021-04-23 17:13:04             🧑  作者: Mango

为您提供了一个从1到N的N个整数的排序数组,其中缺少一个数字,找到丢失的数字。预期时间复杂度O(logn)
例子:

Input :ar[] = {1, 3, 4, 5}
Output : 2

Input : ar[] = {1, 2, 3, 4, 5, 7, 8}
Output : 6

一个简单的解决方案是线性遍历给定数组。找到当前元素不比上一个多的点。
一个有效的解决方案是使用二进制搜索。我们使用索引来搜索缺少的元素,并使用修改后的二进制搜索。如果元素位于中点!= index + 1,并且这是第一个丢失的元素,则中点+ 1是丢失的元素。否则,如果不是不是第一个缺少的元素,而是在左半部分搜索ar [mid]!= mid + 1。否则搜索右半部分,如果从左到右搜索,则没有任何元素丢失。

C++
// CPP program to find the only missing element.
#include 
using namespace std;
 
int findmissing(int ar[], int N)
{
    int l = 0, r = N - 1;
    while (l <= r) {
 
        int mid = (l + r) / 2;
 
        // If this is the first element
        // which is not index + 1, then
        // missing element is mid+1
        if (ar[mid] != mid + 1 &&
                        ar[mid - 1] == mid)
            return mid + 1;
 
        // if this is not the first missing
        // element search in left side
        if (ar[mid] != mid + 1)
            r = mid - 1;
 
        // if it follows index+1 property then
        // search in right side
        else
            l = mid + 1;
    }
 
    // if no element is missing
    return -1;
}
 
// Driver code
int main()
{
    int arr[] = {1, 2, 3, 4, 5, 7, 8};
    int N = sizeof(arr)/sizeof(arr[0]);
    cout << findmissing(arr, N);
    return 0;
}


Java
// Java program to find
// the only missing element.
class GFG
{
static int findmissing(int [] ar, int N)
{
     
    int l = 0, r = N - 1;
    while (l <= r)
    {
        int mid = (l + r) / 2;
     
        // If this is the first element
        // which is not index + 1, then
        // missing element is mid+1
        if (ar[mid] != mid + 1 &&
            ar[mid - 1] == mid)
            return (mid + 1);
     
        // if this is not the first
        // missing element search
        // in left side
        if (ar[mid] != mid + 1)
            r = mid - 1;
     
        // if it follows index+1
        // property then search
        // in right side
        else
            l = mid + 1;
    }
 
// if no element is missing
return -1;
}
 
// Driver code
public static void main(String [] args)
{
    int arr[] = {1, 2, 3, 4, 5, 7, 8};
    int N = arr.length;
    System.out.println(findmissing(arr, N));
}
}
 
// This code is contributed
// by Shivi_Aggarwal


Python3
# PYTHON 3 program to find
# the only missing element.
def findmissing(ar, N):
    l = 0
    r = N - 1
    while (l <= r):
        mid = (l + r) / 2
        mid= int (mid)
 
    # If this is the first element
    # which is not index + 1, then
    # missing element is mid+1
        if(ar[mid] != mid + 1 and
           ar[mid - 1] == mid):
            return (mid + 1)
 
    # if this is not the first
    # missing element search
    # in left side
        elif(ar[mid] != mid + 1):
            r = mid - 1
 
    # if it follows index+1
    # property then search
    # in right side
        else:
            l = mid + 1
     
    # if no element is missing
    return (-1)
     
def main():
    ar= [1, 2, 3, 4, 5, 7, 8]
    N = len(ar)
    res= findmissing(ar, N)
    print (res)
if __name__ == "__main__":
    main()
 
# This code is contributed
# by Shivi_Aggarwal


C#
// C# program to find
// the only missing element.
using System;
 
class GFG
{
static int findmissing(int []ar,
                       int N)
{
     
    int l = 0, r = N - 1;
    while (l <= r)
    {
        int mid = (l + r) / 2;
     
        // If this is the first element
        // which is not index + 1, then
        // missing element is mid+1
        if (ar[mid] != mid + 1 &&
            ar[mid - 1] == mid)
            return (mid + 1);
     
        // if this is not the first
        // missing element search
        // in left side
        if (ar[mid] != mid + 1)
            r = mid - 1;
     
        // if it follows index+1
        // property then search
        // in right side
        else
            l = mid + 1;
    }
 
// if no element is missing
return -1;
}
 
// Driver code
public static void Main()
{
    int []arr = {1, 2, 3, 4, 5, 7, 8};
    int N = arr.Length;
    Console.WriteLine(findmissing(arr, N));
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP


Javascript


输出:
6

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