📜  在包含从1到N的元素的数组中找到四个缺失的数字

📅  最后修改于: 2021-04-26 17:38:07             🧑  作者: Mango

给定唯一整数数组,其中给定数组的每个整数都在[1,N]范围内。数组的大小为(N-4)。不重复单个元素。因此,数组中缺少从1到N的四个数字。按排序顺序找到4个丢失的数字。

例子:

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

Input : arr[] = {1, 7, 3, 13, 5, 10, 8, 4, 9}
Output : 2 6 11 12

一个简单的O(N)解决方案是使用大小为N的辅助数组标记访问的元素。遍历输入数组并标记辅助数组中的元素。最后,打印所有未标记的索引。

如何用O(1)辅助空间求解?
我们初始化一个长度为4的名为辅助程序的数组,以补偿缺少的4个数字并将其填充为零。然后,我们从给定数组的i = 0迭代到i temp的变量中。现在我们将检查:

  1. 如果元素的绝对值小于输入数组的长度,则我们将array [temp]元素乘以-1(以标记访问的元素)。
  2. 如果元素的绝对值大于输入数组的长度,则我们将helper [temp%array.length]元素的值设为-1(以标记访问的元素)。

然后,我们遍历输入数组和那些值仍大于零的索引,然后在输入数组中未遇到那些元素。因此,我们打印元素大于零的索引的(index + 1)值。
然后,我们将遍历helper数组和那些其值仍大于零的索引,然后在输入数组中未遇到那些元素。因此,我们打印元素大于零的索引的(index + array.length + 1)值。

C++
// CPP program to find missing 4 elements
// in an array of size N where elements are
// in range from 1 to N+4.
#include 
using namespace std;
  
// Finds missing 4 numbers in O(N) time
// and O(1) auxiliary space.
void missing4(int arr[], int n)
{
    // To keep track of 4 possible numbers
    // greater than length of input array
    // In Java, helper is automatically
    // initialized as 0.
    int helper[4];
  
    // Traverse the input array and mark
    // visited elements either by marking
    // them as negative in arr[] or in
    // helper[].
    for (int i = 0; i < n; i++) {
        int temp = abs(arr[i]);
  
        // If element is smaller than or
        // equal to length, mark its
        // presence in arr[]
        if (temp <= n)
            arr[temp - 1] *= (-1);
  
        // Mark presence in helper[]
        else if (temp > n) {
            if (temp % n != 0)
                helper[temp % n - 1] = -1;
            else
                helper[(temp % n) + n - 1] = -1;
        }
    }
  
    // Print all those elements whose presence
    // is not marked.
    for (int i = 0; i < n; i++)
        if (arr[i] > 0)
            cout << (i + 1) << " ";
    for (int i = 0; i < 4; i++)
        if (helper[i] >= 0)
            cout << (n + i + 1) << " ";
  
    return;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 7, 3, 12, 5, 10, 8, 4, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
    missing4(arr, n);
    return 0;
}
  
// This code is contributed by Nikita Tiwari.


Java
// Java program to find missing 4 elements
// in an array of size N where elements are
// in range from 1 to N+4.
class Missing4 {
  
    // Finds missing 4 numbers in O(N) time 
    // and O(1) auxiliary space.
    public static void missing4(int[] arr) 
    {
        // To keep track of 4 possible numbers
        // greater than length of input array
        // In Java, helper is automatically
        // initialized as 0.
        int[] helper = new int[4];
  
        // Traverse the input array and mark
        // visited elements either by marking
        // them as negative in arr[] or in 
        // helper[].
        for (int i = 0; i < arr.length; i++) {
            int temp = Math.abs(arr[i]);
  
            // If element is smaller than or 
            // equal to length, mark its 
            // presence in arr[] 
            if (temp <= arr.length) 
                arr[temp - 1] *= (-1);
  
             // Mark presence in helper[]
             else if (temp > arr.length) {
                if (temp % arr.length != 0)
                    helper[temp % arr.length - 1] = -1;
                else
                    helper[(temp % arr.length) + 
                                   arr.length - 1] = -1;
            }
        }
  
        // Print all those elements whose presence
        // is not marked.   
        for (int i = 0; i < arr.length; i++) 
            if (arr[i] > 0) 
                System.out.print(i + 1 + " ");      
        for (int i = 0; i < helper.length; i++) 
            if (helper[i] >= 0) 
                System.out.print(arr.length + i + 1 + " ");            
          
        return;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = { 1, 7, 3, 12, 5, 10, 8, 4, 9 };
        missing4(arr);
    }
}


Python3
# Python 3 program to find missing 4 elements
# in an array of size N where elements are
# in range from 1 to N+4.
  
# Finds missing 4 numbers in O(N) time 
# and O(1) auxiliary space.
def missing4( arr) :
  
    # To keep track of 4 possible numbers
    # greater than length of input array
    # In Java, helper is automatically
    # initialized as 0.
    helper = [0]*4
  
    # Traverse the input array and mark
    # visited elements either by marking
    # them as negative in arr[] or in 
    # helper[].
    for i in range(0,len(arr)) :
        temp = abs(arr[i])
  
        # If element is smaller than or 
        # equal to length, mark its 
        # presence in arr[] 
        if (temp <= len(arr)) :
            arr[temp - 1] = arr[temp - 1] * (-1)
  
        # Mark presence in helper[]
        elif (temp > len(arr)) :
            if (temp % len(arr)) :
                helper[temp % len(arr) - 1] = -1
            else :
                helper[(temp % len(arr)) +len(arr) - 1] = -1
          
          
    # Print all those elements whose presence
    # is not marked.   
    for i in range(0, len(arr) ) :
        if (arr[i] > 0) :
            print((i + 1) , end=" ")
    for i in range(0, len(helper)) :
        if (helper[i] >= 0) :
            print((len(arr) + i + 1) , end=" ") 
              
  
# Driver code
arr = [ 1, 7, 3, 12, 5, 10, 8, 4, 9 ]
missing4(arr)
  
  
# This code is conributed
# by Nikita Tiwari.


C#
// C# program to find missing 4 elements
// in an array of size N where elements are
// in range from 1 to N+4.
using System;
  
class Missing4 {
   
    // Finds missing 4 numbers in O(N) time 
    // and O(1) auxiliary space.
    public static void missing4(int[] arr) 
    {
        // To keep track of 4 possible numbers
        // greater than length of input array
        // In Java, helper is automatically
        // initialized as 0.
        int[] helper = new int[4];
   
        // Traverse the input array and mark
        // visited elements either by marking
        // them as negative in arr[] or in 
        // helper[].
        for (int i = 0; i < arr.Length; i++) {
            int temp = Math.Abs(arr[i]);
   
            // If element is smaller than or 
            // equal to length, mark its 
            // presence in arr[] 
            if (temp <= arr.Length) 
                arr[temp - 1] *= (-1);
   
             // Mark presence in helper[]
             else if (temp > arr.Length) {
                if (temp % arr.Length != 0)
                    helper[temp % arr.Length - 1] = -1;
                else
                    helper[(temp % arr.Length) + 
                                   arr.Length - 1] = -1;
            }
        }
   
        // Print all those elements whose presence
        // is not marked.   
        for (int i = 0; i < arr.Length; i++) 
            if (arr[i] > 0) 
                Console.Write(i + 1 + " ");      
        for (int i = 0; i < helper.Length; i++) 
            if (helper[i] >= 0) 
                Console.Write(arr.Length + i + 1 + " ");            
           
        return;
    }
   
    // Driver code
    public static void Main()
    {
        int[] arr = { 1, 7, 3, 12, 5, 10, 8, 4, 9 };
        missing4(arr);
    }
}
  
//This code is contributed by Anant Agarwal.


PHP
 $n) 
        {
            if ($temp % $n != 0)
                $helper[$temp % $n - 1] = -1;
            else
                $helper[($temp % $n) + $n - 1] = -1;
        }
    }
  
    // Print all those elements whose 
    // presence is not marked.
    for ($i = 0; $i < $n; $i++)
        if ($arr[$i] > 0)
        {
            $a = $i + 1;
            echo "$a", " ";
        }
    for ($i = 0; $i < 4; $i++)
        if ($helper[$i] >= 0)
        {
            $b = $n + $i + 1;
            echo "$b", " ";
        }
        echo "\n";
  
    return;
}
  
// Driver code
$arr = array(1, 7, 3, 12, 5, 10, 8, 4, 9);
$n = sizeof($arr);
missing4($arr, $n);
  
// This code is contributed by iAyushRaj
?>


输出:

2 6 11 13 

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