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

📅  最后修改于: 2021-04-21 23:43:42             🧑  作者: Mango

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

例子:

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

  1. 初始化变量diff ,该变量等于arr [0] – 0
  2. 现在遍历数组,看看arr [i] – idiff之间的是否为零。
  3. 如果上述步骤中的差不等于零,则找到丢失的元素。
  4. 要查找多个丢失的元素,请在其中运行一个循环,看看diff是否小于arr [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


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


输出:
8 9 12


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

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

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

下面是上述方法的实现:

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
输出:
8 9 12


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