原文:

给定一个整数数组,打印 0-99 范围内缺失的元素。如果有一个以上的缺失,整理它们,否则只打印数字。 注意,输入数组可能没有排序,可能包含范围[0-99]之外的数字,但打印缺失元素时只考虑这个范围。

示例:

input: {88, 105, 3, 2, 200, 0, 10}
output: 1
        4-9
        11-87
        89-99
input: {9, 6, 900, 850, 5, 90, 100, 99}
output: 0-4
        7-8
        10-89
        91-98

预期时间复杂度 o(n),其中 n 是输入数组的大小。

想法是使用大小为 100 的布尔数组来跟踪 0 到 99 范围内的数组元素。我们首先遍历输入数组,并在布尔数组中标记这些当前元素。一旦标记了所有存在的元素,布尔数组就被用来打印丢失的元素。

以下是上述想法的实现。

c

// c   program for print missing elements
#include 
#define limit 100
using namespace std;
// a o(n) function to print missing elements in an array
void printmissing(int arr[], int n)
{
    // initialize all number from 0 to 99 as not seen
    bool seen[limit] = {false};
    // mark present elements in range [0-99] as seen
    for (int i=0; i

c

// c program for print missing elements
#include
#define limit 100
// a o(n) function to print missing elements in an array
void printmissing(int arr[], int n)
{
    // initialize all number from 0 to 99 as not seen
    bool seen[limit] = {false};
    // mark present elements in range [0-99] as seen
    for (int i=0; i

java 语言(一种计算机语言,尤用于创建网站)

class printmissingelement
{
    // a o(n) function to print missing elements in an array
    void printmissing(int arr[], int n)
    {
        int limit = 100;
        boolean seen[] = new boolean[limit];
        // initialize all number from 0 to 99 as not seen
        for (int i = 0; i < limit; i  )
            seen[i] = false;
        // mark present elements in range [0-99] as seen
        for (int i = 0; i < n; i  )
        {
            if (arr[i] < limit)
                seen[arr[i]] = true;
        }
        // print missing element
        int i = 0;
        while (i < limit)
        {
            // if i is missing
            if (seen[i] == false)
            {
                // find if there are more missing elements after i
                int j = i   1;
                while (j < limit && seen[j] == false)
                    j  ;
                // print missing single or range
                int p = j-1;
                system.out.println(i 1==j ? i : i   "-"   p);
                // update u
                i = j;
            }
            else
                i  ;
        }
    }
    // driver program to test above functions
    public static void main(string[] args)
    {
        printmissingelement missing = new printmissingelement();
        int arr[] = {88, 105, 3, 2, 200, 0, 10};
        int n = arr.length;
        missing.printmissing(arr, n);
    }
}

python 3

# python3 program for print missing elements
# a o(n) function to print missing elements in an array
def printmissing(arr, n) : 
    limit = 100
    seen = [false]*limit
    # initialize all number from 0 to 99 as not seen
    for i in range(limit) :
      seen[i] = false
    # mark present elements in range [0-99] as seen
    for i in range(n) :
      if (arr[i] < limit) :
        seen[arr[i]] = true
    # print missing element
    i = 0
    while (i < limit) :
      # if i is missing
      if (seen[i] == false) :
        # find if there are more missing elements after i
        j = i   1
        while (j < limit and seen[j] == false) :
          j  = 1
        # print missing single or range
        p = j - 1
        if(i   1 == j) :   
          print(i)
        else :
          print(i, "-", p)
        # update u
        i = j
      else :
        i  = 1
  # driver code
arr = [88, 105, 3, 2, 200, 0, 10]
n = len(arr)
printmissing(arr, n)
# this code is contributed by divyesh072019.

c

using system;
class gfg
{
  // a o(n) function to print missing elements in an array
  static void printmissing(int[] arr, int n) 
  {
    int limit = 100;
    bool[] seen = new bool[limit];
    int i;
    // initialize all number from 0 to 99 as not seen
    for (i = 0; i < limit; i  ) 
      seen[i] = false;
    // mark present elements in range [0-99] as seen
    for (i = 0; i < n; i  ) 
    {
      if (arr[i] < limit)
        seen[arr[i]] = true;
    }
    // print missing element
    i = 0;
    while (i < limit) 
    {
      // if i is missing
      if (seen[i] == false) 
      {
        // find if there are more missing elements after i
        int j = i   1;
        while (j < limit && seen[j] == false)
          j  ;
        // print missing single or range
        int p = j - 1;
        if(i   1 == j)
        {
          console.writeline(i);
        }
        else
        {
          console.writeline(i   "-"   p);
        }
        // update u
        i = j;
      } 
      else
        i  ;
    }
  }
  // driver code
  static void main()
  {
    int[] arr = {88, 105, 3, 2, 200, 0, 10};
    int n = arr.length;
    printmissing(arr, n);
  }
}
// this code is contributed by divyeshrabadiya07.

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


输出:

1
4-9
11-87
89-99

上述程序的时间复杂度为 o(n)。

本文由供稿。如果你发现任何不正确的地方,或者你想分享更多关于上面讨论的话题的信息,请写评论。