📌  相关文章
📜  从给定的排序数组中查找所有缺失的数字

📅  最后修改于: 2021-10-27 08:58:39             🧑  作者: Mango

给定一个由N 个整数组成的排序数组arr[] ,任务是在[arr[0], arr[N-1]]范围之间找到数组中的多个缺失元素。

例子:

朴素的方法:朴素的想法是迭代连续元素对之间的差异,如果差异不为零,则打印此范围内的所有数字。以下是步骤:

  1. 初始化等于arr[0] – 0的变量diff
  2. 现在遍历数组,看看arr[i] – idiff之间的差异是否为零。
  3. 如果上述步骤中的差值不为零,则找到丢失的元素。
  4. 要找到多个丢失的元素,请在其中运行一个循环,看看diff是否小于arr[i] – i然后打印丢失的元素,即i + diff
  5. 现在递增DIFF方面的差异正在增加。
  6. 从第 2 步开始重复,直到找不到所有丢失的数字。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the missing elements
void printMissingElements(int arr[], int N)
{
 
    // Initialize diff
    int diff = arr[0] - 0;
 
    for (int i = 0; i < N; i++) {
 
        // Check if diff and arr[i]-i
        // both are equal or not
        if (arr[i] - i != diff) {
 
            // Loop for consecutive
            // missing elements
            while (diff < arr[i] - i) {
                cout << i + diff << " ";
                diff++;
            }
        }
    }
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 6, 7, 10, 11, 13 };
 
    int N = sizeof(arr) / sizeof(int);
 
    // Function Call
    printMissingElements(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find the missing elements
static void printMissingElements(int arr[],
                                 int N)
{
 
    // Initialize diff
    int diff = arr[0] - 0;
 
    for(int i = 0; i < N; i++)
    {
 
        // Check if diff and arr[i]-i
        // both are equal or not
        if (arr[i] - i != diff)
        {
 
            // Loop for consecutive
            // missing elements
            while (diff < arr[i] - i)
            {
                System.out.print((i + diff) + " ");
                diff++;
            }
        }
    }
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given array arr[]
    int arr[] = { 6, 7, 10, 11, 13 };
     
    int N = arr.length;
     
    // Function call
    printMissingElements(arr, N);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
 
# Function to find the missing elements
def printMissingElements(arr, N):
 
    # Initialize diff
    diff = arr[0]
 
    for i in range(N):
 
        # Check if diff and arr[i]-i
        # both are equal or not
        if(arr[i] - i != diff):
 
            # Loop for consecutive
            # missing elements
            while(diff < arr[i] - i):
                print(i + diff, end = " ")
                diff += 1
 
# Driver Code
 
# Given array arr[]
arr = [ 6, 7, 10, 11, 13 ]
 
N = len(arr)
 
# Function call
printMissingElements(arr, N)
 
# This code is contributed by Shivam Singh


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the missing elements
static void printMissingElements(int[] arr,
                                 int N)
{
     
    // Initialize diff
    int diff = arr[0] - 0;
 
    for(int i = 0; i < N; i++)
    {
         
        // Check if diff and arr[i]-i
        // both are equal or not
        if (arr[i] - i != diff)
        {
             
            // Loop for consecutive
            // missing elements
            while (diff < arr[i] - i)
            {
                Console.Write(i + diff + " ");
                diff++;
            }
        }
    }
}
 
// Driver code
static void Main()
{
     
    // Given array arr[]
    int[] arr = { 6, 7, 10, 11, 13 };
     
    int N = arr.Length;
     
    // Function call
    printMissingElements(arr, N);
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the missing elements
void printMissingElements(int arr[], int N)
{
    // Initialize an array with zero
    // of size equals to the maximum
    // element in the array
    int b[arr[N - 1] + 1] = { 0 };
 
    // Make b[i]=1 if i is present
    // in the array
    for (int i = 0; i < N; i++) {
 
        // If the element is present
        // make b[arr[i]]=1
        b[arr[i]] = 1;
    }
 
    // Print the indices where b[i]=0
    for (int i = arr[0]; i <= arr[N - 1]; i++) {
 
        if (b[i] == 0) {
            cout << i << " ";
        }
    }
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 6, 7, 10, 11, 13 };
 
    int N = sizeof(arr) / sizeof(int);
 
    // Function Call
    printMissingElements(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find the missing elements
static void printMissingElements(int arr[],
                                int N)
{
     
    // Initialize an array with zero
    // of size equals to the maximum
    // element in the array
    int[] b = new int[arr[N - 1] + 1];
 
    // Make b[i]=1 if i is present
    // in the array
    for(int i = 0; i < N; i++)
    {
         
        // If the element is present
        // make b[arr[i]]=1
        b[arr[i]] = 1;
    }
 
    // Print the indices where b[i]=0
    for(int i = arr[0]; i <= arr[N - 1]; i++)
    {
        if (b[i] == 0)
        {
            System.out.print(i + " ");
        }
    }
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given array arr[]
    int arr[] = { 6, 7, 10, 11, 13 };
     
    int N = arr.length;
     
    // Function call
    printMissingElements(arr, N);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to implement
# the above approach
 
# Function to find the missing elements
def printMissingElements(arr, N):
 
    # Initialize an array with zero
    # of size equals to the maximum
    # element in the array
    b = [0] * (arr[N - 1] + 1)
 
    # Make b[i]=1 if i is present
    # in the array
    for i in range(N):
 
        # If the element is present
        # make b[arr[i]]=1
        b[arr[i]] = 1
 
    # Print the indices where b[i]=0
    for i in range(arr[0], arr[N - 1] + 1):
        if(b[i] == 0):
            print(i, end = " ")
 
# Driver Code
 
# Given array arr[]
arr = [ 6, 7, 10, 11, 13 ]
 
N = len(arr)
 
# Function call
printMissingElements(arr, N)
 
# This code is contributed by Shivam Singh


C#
// C# program for
// the above approach
using System;
class GFG{
     
// Function to find the missing elements
static void printMissingElements(int []arr,
                                 int N)
{    
  // Initialize an array with zero
  // of size equals to the maximum
  // element in the array
  int[] b = new int[arr[N - 1] + 1];
 
  // Make b[i]=1 if i is present
  // in the array
  for(int i = 0; i < N; i++)
  {
    // If the element is present
    // make b[arr[i]]=1
    b[arr[i]] = 1;
  }
 
  // Print the indices where b[i]=0
  for(int i = arr[0]; i <= arr[N - 1];
          i++)
  {
    if (b[i] == 0)
    {
      Console.Write(i + " ");
    }
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given array []arr
  int []arr = {6, 7, 10, 11, 13};
 
  int N = arr.Length;
 
  // Function call
  printMissingElements(arr, N);
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
8 9 12

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

高效的方法:想法是使用Hashing来优化上述方法。创建一个大小等于数组中最大元素的布尔数组(比如b[] ),并仅标记数组 b[] 中存在于给定数组中的那些位置。打印数组b[]中所有未标记的索引。
以下是步骤:

  1. 初始化一个布尔数组b[] ,其大小为零,等于数组的最大元素。
  2. 迭代给定数组并标记给定数组中的每个元素,在数组b[]中将该索引标记为真。
  3. 现在从索引arr[0]遍历给定数组b[]并打印那些值为 false 的索引,因为它们是给定数组中缺少的元素。

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the missing elements
void printMissingElements(int arr[], int N)
{
    // Initialize an array with zero
    // of size equals to the maximum
    // element in the array
    int b[arr[N - 1] + 1] = { 0 };
 
    // Make b[i]=1 if i is present
    // in the array
    for (int i = 0; i < N; i++) {
 
        // If the element is present
        // make b[arr[i]]=1
        b[arr[i]] = 1;
    }
 
    // Print the indices where b[i]=0
    for (int i = arr[0]; i <= arr[N - 1]; i++) {
 
        if (b[i] == 0) {
            cout << i << " ";
        }
    }
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 6, 7, 10, 11, 13 };
 
    int N = sizeof(arr) / sizeof(int);
 
    // Function Call
    printMissingElements(arr, N);
 
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find the missing elements
static void printMissingElements(int arr[],
                                int N)
{
     
    // Initialize an array with zero
    // of size equals to the maximum
    // element in the array
    int[] b = new int[arr[N - 1] + 1];
 
    // Make b[i]=1 if i is present
    // in the array
    for(int i = 0; i < N; i++)
    {
         
        // If the element is present
        // make b[arr[i]]=1
        b[arr[i]] = 1;
    }
 
    // Print the indices where b[i]=0
    for(int i = arr[0]; i <= arr[N - 1]; i++)
    {
        if (b[i] == 0)
        {
            System.out.print(i + " ");
        }
    }
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given array arr[]
    int arr[] = { 6, 7, 10, 11, 13 };
     
    int N = arr.length;
     
    // Function call
    printMissingElements(arr, N);
}
}
 
// This code is contributed by offbeat

蟒蛇3

# Python3 program to implement
# the above approach
 
# Function to find the missing elements
def printMissingElements(arr, N):
 
    # Initialize an array with zero
    # of size equals to the maximum
    # element in the array
    b = [0] * (arr[N - 1] + 1)
 
    # Make b[i]=1 if i is present
    # in the array
    for i in range(N):
 
        # If the element is present
        # make b[arr[i]]=1
        b[arr[i]] = 1
 
    # Print the indices where b[i]=0
    for i in range(arr[0], arr[N - 1] + 1):
        if(b[i] == 0):
            print(i, end = " ")
 
# Driver Code
 
# Given array arr[]
arr = [ 6, 7, 10, 11, 13 ]
 
N = len(arr)
 
# Function call
printMissingElements(arr, N)
 
# This code is contributed by Shivam Singh

C#

// C# program for
// the above approach
using System;
class GFG{
     
// Function to find the missing elements
static void printMissingElements(int []arr,
                                 int N)
{    
  // Initialize an array with zero
  // of size equals to the maximum
  // element in the array
  int[] b = new int[arr[N - 1] + 1];
 
  // Make b[i]=1 if i is present
  // in the array
  for(int i = 0; i < N; i++)
  {
    // If the element is present
    // make b[arr[i]]=1
    b[arr[i]] = 1;
  }
 
  // Print the indices where b[i]=0
  for(int i = arr[0]; i <= arr[N - 1];
          i++)
  {
    if (b[i] == 0)
    {
      Console.Write(i + " ");
    }
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given array []arr
  int []arr = {6, 7, 10, 11, 13};
 
  int N = arr.Length;
 
  // Function call
  printMissingElements(arr, N);
}
}
 
// This code is contributed by Princi Singh

Javascript


输出:
8 9 12

时间复杂度: O(M),其中 M 是数组的最大元素。
辅助空间: O(M)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程