📜  查找丢失的号码

📅  最后修改于: 2021-05-07 08:47:00             🧑  作者: Mango

系统会为您提供n-1个整数的列表,这些整数在1到n的范围内。列表中没有重复项。列表中缺少整数之一。编写有效的代码以查找丢失的整数。
例子:

Input: arr[] = {1, 2, 4, 6, 3, 7, 8}
Output: 5
Explanation: The missing number from 1 to 8 is 5

Input: arr[] = {1, 2, 3, 5}
Output: 4
Explanation: The missing number from 1 to 5 is 4

方法1此方法使用求和公式的技术。

  • 方法:数组的长度为n-1。因此,可以使用公式n *(n + 1)/ 2计算所有n个元素的总和,即从1到n的数字之和。现在找到数组中所有元素的总和,并从前n个自然数的总和中减去它,这将是缺失元素的值。
  • 算法:
    1. 计算前n个自然数的总和,即sumtotal = n *(n + 1)/ 2
    2. 创建一个变量sum来存储数组元素的总和。
    3. 从头到尾遍历数组。
    4. 将sum的值更新为sum = sum + array [i]
    5. 将缺少的数字打印为汇总–总和
  • 执行:
C++
#include 
using namespace std;
 
// Function to get the missing number
int getMissingNo(int a[], int n)
{
 
    int total = (n + 1) * (n + 2) / 2;
    for (int i = 0; i < n; i++)
        total -= a[i];
    return total;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int miss = getMissingNo(arr, n);
    cout << miss;
}


C
#include 
 
/* getMissingNo takes array and size of array as arguments*/
int getMissingNo(int a[], int n)
{
    int i, total;
    total = (n + 1) * (n + 2) / 2;
    for (i = 0; i < n; i++)
        total -= a[i];
    return total;
}
 
/*program to test above function */
int main()
{
    int a[] = { 1, 2, 4, 5, 6 };
    int miss = getMissingNo(a, 5);
    printf("%d", miss);
    getchar();
}


Java
// Java program to find missing Number
import java.util.Arrays;
import java.util.*;
   class GFG{
    public static List findDisappearedNumbers(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            int index = Math.abs(nums[i]);
            if (nums[index - 1] > 0) {
                nums[index - 1] *= -1;
            }
        }
        List res = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0) {
                res.add(i + 1);
            }
        }
        return res;
    }
      public static void main(String[] args) {
       int [] a ={1,2,2,3,1};
       System.out.println(findDisappearedNumbers(a));
      }
  }


Python
# getMissingNo takes list as argument
def getMissingNo(A):
    n = len(A)
    total = (n + 1)*(n + 2)/2
    sum_of_A = sum(A)
    return total - sum_of_A
 
# Driver program to test the above function
A = [1, 2, 4, 5, 6]
miss = getMissingNo(A)
print(miss)
# This code is contributed by Pratik Chhajer


C#
// C# program to find missing Number
using System;
 
class GFG {
    // Function to ind missing number
    static int getMissingNo(int[] a, int n)
    {
        int total = (n + 1) * (n + 2) / 2;
 
        for (int i = 0; i < n; i++)
            total -= a[i];
 
        return total;
    }
 
    /* program to test above function */
    public static void Main()
    {
        int[] a = { 1, 2, 4, 5, 6 };
        int miss = getMissingNo(a, 5);
        Console.Write(miss);
    }
}
 
// This code is contributed by Sam007_


PHP


Javascript


C++
#include 
using namespace std;
 
// a represents the array
// n : Number of elements in array a
int getMissingNo(int a[], int n)
{
    int i, total=1;
     
    for ( i = 2; i<= (n+1); i++)
    {
        total+=i;
        total -= a[i-2];
    }
    return total;
}
 
//Driver Program
int main() {
    int arr[] = {1, 2, 3, 5};
    cout<


Java
// Java implementation
class GFG
{
 
    // a represents the array
    // n : Number of elements in array a
    static int getMissingNo(int a[], int n)
    {
        int total = 1;
        for (int i = 2; i <= (n + 1); i++)
        {
            total += i;
            total -= a[i - 2];
        }
        return total;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 1, 2, 3, 5 };
        System.out.println(getMissingNo(arr, arr.length));
    }
}
 
// This post is contributed
// by Vivek Kumar Singh


Python3
# a represents the array
# n : Number of elements in array a
def getMissingNo(a, n):
    i, total = 0, 1
 
    for i in range(2, n + 2):
        total += i
        total -= a[i - 2]
    return total
 
# Driver Code
arr = [1, 2, 3, 5]
print(getMissingNo(arr, len(arr)))
 
# This code is contributed by Mohit kumar


C#
using System;
 
class GFG
{
     
// a represents the array
// n : Number of elements in array a
static int getMissingNo(int[] a, int n)
{
    int i, total = 1;
     
    for ( i = 2; i <= (n + 1); i++)
    {
        total += i;
        total -= a[i - 2];
    }
    return total;
}
 
// Driver Code
public static void Main()
{
    int[] arr = {1, 2, 3, 5};
    Console.Write(getMissingNo(arr, (arr.Length)));
 
    // Console.Write(getMissingNo(arr, 4));
}
}
// This code is contributed by SoumikMondal


Javascript


C++
#include 
using namespace std;
 
// Function to get the missing number
int getMissingNo(int a[], int n)
{
    // For xor of all the elements in array
    int x1 = a[0];
 
    // For xor of all the elements from 1 to n+1
    int x2 = 1;
 
    for (int i = 1; i < n; i++)
        x1 = x1 ^ a[i];
 
    for (int i = 2; i <= n + 1; i++)
        x2 = x2 ^ i;
 
    return (x1 ^ x2);
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int miss = getMissingNo(arr, n);
    cout << miss;
}


C
#include 
 
/* getMissingNo takes array and size of array as arguments*/
int getMissingNo(int a[], int n)
{
    int i;
    int x1 = a[0]; /* For xor of all the elements in array */
    int x2 = 1; /* For xor of all the elements from 1 to n+1 */
 
    for (i = 1; i < n; i++)
        x1 = x1 ^ a[i];
 
    for (i = 2; i <= n + 1; i++)
        x2 = x2 ^ i;
 
    return (x1 ^ x2);
}
 
/*program to test above function */
int main()
{
    int a[] = { 1, 2, 4, 5, 6 };
    int miss = getMissingNo(a, 5);
    printf("%d", miss);
    getchar();
}


Java
// Java program to find missing Number
// using xor
class Main {
    // Function to find missing number
    static int getMissingNo(int a[], int n)
    {
        int x1 = a[0];
        int x2 = 1;
 
        /* For xor of all the elements
           in array */
        for (int i = 1; i < n; i++)
            x1 = x1 ^ a[i];
 
        /* For xor of all the elements
           from 1 to n+1 */
        for (int i = 2; i <= n + 1; i++)
            x2 = x2 ^ i;
 
        return (x1 ^ x2);
    }
 
    /* program to test above function */
    public static void main(String args[])
    {
        int a[] = { 1, 2, 4, 5, 6 };
        int miss = getMissingNo(a, 5);
        System.out.println(miss);
    }
}


Python3
# Python3 program to find
# the mising Number
# getMissingNo takes list as argument
 
def getMissingNo(a, n):
    x1 = a[0]
    x2 = 1
     
    for i in range(1, n):
        x1 = x1 ^ a[i]
         
    for i in range(2, n + 2):
        x2 = x2 ^ i
     
    return x1 ^ x2
 
 
# Driver program to test above function
if __name__=='__main__':
 
    a = [1, 2, 4, 5, 6]
    n = len(a)
    miss = getMissingNo(a, n)
    print(miss)
     
# This code is contributed by Yatin Gupta


C#
// C# program to find missing Number
// using xor
using System;
 
class GFG {
    // Function to find missing number
    static int getMissingNo(int[] a, int n)
    {
        int x1 = a[0];
        int x2 = 1;
 
        /* For xor of all the elements
        in array */
        for (int i = 1; i < n; i++)
            x1 = x1 ^ a[i];
 
        /* For xor of all the elements
        from 1 to n+1 */
        for (int i = 2; i <= n + 1; i++)
            x2 = x2 ^ i;
 
        return (x1 ^ x2);
    }
 
    /* driver program to test above function */
    public static void Main()
    {
        int[] a = { 1, 2, 4, 5, 6 };
        int miss = getMissingNo(a, 5);
        Console.Write(miss);
    }
}
 
// This code is contributed by Sam007_


PHP


Javascript


C++
// C++ program to find
// the mising Number
#include
using namespace std;
 
// getMissingNo takes list
// as argument
int getMissingNo(int a[] ,
                 int n)
{
  int n_elements_sum = n * (n + 1) / 2 ;
  int sum = 0;
 
  for(int i = 0; i < n - 1; i++)
    sum += a[i];
  return n_elements_sum-sum;
}
 
// Driver code
int main()
{
  int a[] = {1, 2, 4, 5, 6};
  int n = sizeof(a) /
          sizeof(a[0]) + 1;
  int miss = getMissingNo(a, n);
  cout << (miss);
  return 0;
}
 
// This code is contributed by Chitranayal


Java
// Java program to find
// the missing Number
class GFG{
 
// getMissingNo function for
// finding missing number
static int getMissingNo(int a[], int n)
{
    int n_elements_sum = n * (n + 1) / 2;
    int sum = 0;
 
    for(int i = 0; i < n - 1; i++)
        sum += a[i];
         
    return n_elements_sum - sum;
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { 1, 2, 4, 5, 6 };
    int n = a.length + 1;
    int miss = getMissingNo(a, n);
     
    System.out.print(miss);
}
}
 
// This code is contributed by mark_85


Python3
# Python3 program to find
# the mising Number
# getMissingNo takes list as argument
def getMissingNo(a, n):
    n_elements_sum=n*(n+1)//2
    return n_elements_sum-sum(a)
 
 
# Driver program to test above function
if __name__=='__main__':
 
    a = [1, 2, 4, 5, 6]
    n = len(a)+1
    miss = getMissingNo(a, n)
    print(miss)


C#
// C# program to implement
// the above approach
using System;
class GFG{
   
// Function to find missing
// number
static int getMissingNo(int[] a,
                        int n)
{
  int n_elements_sum = (n * (n + 1) / 2);
  int sum = 0;
 
  for (int i = 0; i < n - 1; i++)
    sum = sum + a[i];
 
  return (n_elements_sum - sum);
}
 
// Driver code
public static void Main()
{
  int[] a = {1, 2, 4, 5, 6};
  int miss = getMissingNo(a, 5);
  Console.Write(miss);
}
}
 
// This code is contributed by Virusbuddah


Javascript


输出:
3
  • 复杂度分析:
    • 时间复杂度: O(n)。
      只需要遍历数组一次。
    • 空间复杂度: O(1)。
      无需额外空间

修改溢出

  • 方法:方法保持不变,但是如果n大,则可能会溢出。为了避免整数溢出,请从已知数字中选择一个数字,然后从给定数字中减去一个数字。这样,就永远不会有整数溢出。
  • 算法:
    1. 创建一个变量sum = 1 ,该变量将存储缺少的数字和一个计数器c = 2
    2. 从头到尾遍历数组。
    3. 将sum的值更新为sum = sum – array [i] + c,并将c更新为C++
    4. 打印缺少的数字作为总和

C++

#include 
using namespace std;
 
// a represents the array
// n : Number of elements in array a
int getMissingNo(int a[], int n)
{
    int i, total=1;
     
    for ( i = 2; i<= (n+1); i++)
    {
        total+=i;
        total -= a[i-2];
    }
    return total;
}
 
//Driver Program
int main() {
    int arr[] = {1, 2, 3, 5};
    cout<

Java

// Java implementation
class GFG
{
 
    // a represents the array
    // n : Number of elements in array a
    static int getMissingNo(int a[], int n)
    {
        int total = 1;
        for (int i = 2; i <= (n + 1); i++)
        {
            total += i;
            total -= a[i - 2];
        }
        return total;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 1, 2, 3, 5 };
        System.out.println(getMissingNo(arr, arr.length));
    }
}
 
// This post is contributed
// by Vivek Kumar Singh

Python3

# a represents the array
# n : Number of elements in array a
def getMissingNo(a, n):
    i, total = 0, 1
 
    for i in range(2, n + 2):
        total += i
        total -= a[i - 2]
    return total
 
# Driver Code
arr = [1, 2, 3, 5]
print(getMissingNo(arr, len(arr)))
 
# This code is contributed by Mohit kumar

C#

using System;
 
class GFG
{
     
// a represents the array
// n : Number of elements in array a
static int getMissingNo(int[] a, int n)
{
    int i, total = 1;
     
    for ( i = 2; i <= (n + 1); i++)
    {
        total += i;
        total -= a[i - 2];
    }
    return total;
}
 
// Driver Code
public static void Main()
{
    int[] arr = {1, 2, 3, 5};
    Console.Write(getMissingNo(arr, (arr.Length)));
 
    // Console.Write(getMissingNo(arr, 4));
}
}
// This code is contributed by SoumikMondal

Java脚本


输出:
4
  • 复杂度分析:
    • 时间复杂度: O(n)。
      只需要遍历数组一次。
    • 空间复杂度: O(1)。
      无需额外空间

感谢Sahil Rally提出了这一改进建议。
方法2此方法使用XOR技术解决问题。

  • 方法:
    XOR具有某些属性
    • 假设a1 ^ a2 ^ a3 ^…^ an = a和a1 ^ a2 ^ a3 ^…^ an-1 = b
    • 然后a ^ b = an
  • 算法:
    1. 创建两个变量a = 0b = 0
    2. 以i为计数器从1到n循环运行。
    3. 对于每一个索引更新一个作为一个= ^ I
    4. 现在,从头到尾遍历数组。
    5. 对于每个索引更新b,因为b = b ^ array [i]
    6. 将缺少的数字打印为a ^ b
  • 执行:

C++

#include 
using namespace std;
 
// Function to get the missing number
int getMissingNo(int a[], int n)
{
    // For xor of all the elements in array
    int x1 = a[0];
 
    // For xor of all the elements from 1 to n+1
    int x2 = 1;
 
    for (int i = 1; i < n; i++)
        x1 = x1 ^ a[i];
 
    for (int i = 2; i <= n + 1; i++)
        x2 = x2 ^ i;
 
    return (x1 ^ x2);
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int miss = getMissingNo(arr, n);
    cout << miss;
}

C

#include 
 
/* getMissingNo takes array and size of array as arguments*/
int getMissingNo(int a[], int n)
{
    int i;
    int x1 = a[0]; /* For xor of all the elements in array */
    int x2 = 1; /* For xor of all the elements from 1 to n+1 */
 
    for (i = 1; i < n; i++)
        x1 = x1 ^ a[i];
 
    for (i = 2; i <= n + 1; i++)
        x2 = x2 ^ i;
 
    return (x1 ^ x2);
}
 
/*program to test above function */
int main()
{
    int a[] = { 1, 2, 4, 5, 6 };
    int miss = getMissingNo(a, 5);
    printf("%d", miss);
    getchar();
}

Java

// Java program to find missing Number
// using xor
class Main {
    // Function to find missing number
    static int getMissingNo(int a[], int n)
    {
        int x1 = a[0];
        int x2 = 1;
 
        /* For xor of all the elements
           in array */
        for (int i = 1; i < n; i++)
            x1 = x1 ^ a[i];
 
        /* For xor of all the elements
           from 1 to n+1 */
        for (int i = 2; i <= n + 1; i++)
            x2 = x2 ^ i;
 
        return (x1 ^ x2);
    }
 
    /* program to test above function */
    public static void main(String args[])
    {
        int a[] = { 1, 2, 4, 5, 6 };
        int miss = getMissingNo(a, 5);
        System.out.println(miss);
    }
}

Python3

# Python3 program to find
# the mising Number
# getMissingNo takes list as argument
 
def getMissingNo(a, n):
    x1 = a[0]
    x2 = 1
     
    for i in range(1, n):
        x1 = x1 ^ a[i]
         
    for i in range(2, n + 2):
        x2 = x2 ^ i
     
    return x1 ^ x2
 
 
# Driver program to test above function
if __name__=='__main__':
 
    a = [1, 2, 4, 5, 6]
    n = len(a)
    miss = getMissingNo(a, n)
    print(miss)
     
# This code is contributed by Yatin Gupta

C#

// C# program to find missing Number
// using xor
using System;
 
class GFG {
    // Function to find missing number
    static int getMissingNo(int[] a, int n)
    {
        int x1 = a[0];
        int x2 = 1;
 
        /* For xor of all the elements
        in array */
        for (int i = 1; i < n; i++)
            x1 = x1 ^ a[i];
 
        /* For xor of all the elements
        from 1 to n+1 */
        for (int i = 2; i <= n + 1; i++)
            x2 = x2 ^ i;
 
        return (x1 ^ x2);
    }
 
    /* driver program to test above function */
    public static void Main()
    {
        int[] a = { 1, 2, 4, 5, 6 };
        int miss = getMissingNo(a, 5);
        Console.Write(miss);
    }
}
 
// This code is contributed by Sam007_

的PHP


Java脚本


输出:
3
  • 复杂度分析:
    • 时间复杂度: O(n)。
      只需要遍历数组一次。
    • 空间复杂度: O(1)。
      不需要额外的空间。

方法3此方法仅在Python。
方法:
取数组中所有元素的总和,然后从n + 1个元素的总和中减去。例如:
如果我的元素是li = [1,2,4,5],则取li中所有元素的总和,然后从len(li)+1个元素的总和中减去。以下代码显示了该示例。

C++

// C++ program to find
// the mising Number
#include
using namespace std;
 
// getMissingNo takes list
// as argument
int getMissingNo(int a[] ,
                 int n)
{
  int n_elements_sum = n * (n + 1) / 2 ;
  int sum = 0;
 
  for(int i = 0; i < n - 1; i++)
    sum += a[i];
  return n_elements_sum-sum;
}
 
// Driver code
int main()
{
  int a[] = {1, 2, 4, 5, 6};
  int n = sizeof(a) /
          sizeof(a[0]) + 1;
  int miss = getMissingNo(a, n);
  cout << (miss);
  return 0;
}
 
// This code is contributed by Chitranayal

Java

// Java program to find
// the missing Number
class GFG{
 
// getMissingNo function for
// finding missing number
static int getMissingNo(int a[], int n)
{
    int n_elements_sum = n * (n + 1) / 2;
    int sum = 0;
 
    for(int i = 0; i < n - 1; i++)
        sum += a[i];
         
    return n_elements_sum - sum;
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { 1, 2, 4, 5, 6 };
    int n = a.length + 1;
    int miss = getMissingNo(a, n);
     
    System.out.print(miss);
}
}
 
// This code is contributed by mark_85

Python3

# Python3 program to find
# the mising Number
# getMissingNo takes list as argument
def getMissingNo(a, n):
    n_elements_sum=n*(n+1)//2
    return n_elements_sum-sum(a)
 
 
# Driver program to test above function
if __name__=='__main__':
 
    a = [1, 2, 4, 5, 6]
    n = len(a)+1
    miss = getMissingNo(a, n)
    print(miss)

C#

// C# program to implement
// the above approach
using System;
class GFG{
   
// Function to find missing
// number
static int getMissingNo(int[] a,
                        int n)
{
  int n_elements_sum = (n * (n + 1) / 2);
  int sum = 0;
 
  for (int i = 0; i < n - 1; i++)
    sum = sum + a[i];
 
  return (n_elements_sum - sum);
}
 
// Driver code
public static void Main()
{
  int[] a = {1, 2, 4, 5, 6};
  int miss = getMissingNo(a, 5);
  Console.Write(miss);
}
}
 
// This code is contributed by Virusbuddah

Java脚本


输出:

3

时间复杂度: O(n)