查找数组中的缺失的2个数字

Problem

Two numbers are missing from the first hundred numbers. They are NOT sorted. How to find them? 
You can't sort.. and can't iterate one by one.. has to be less than O(N)? Can't use stack , set or any collection interface or in fact any other data structure or array!

Solution
// FindTwoMissingNumbers.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstdlib>

using namespace std;

void InitArray(int* arr)
{
    vector<int> vec;
    int num1 = rand() % 100;
    int num2 = num1;

    while(num1 == num2){
        num2 = rand() % 100 ;
    }

    int smallMissingNum = (num1 < num2) ? num1 : num2;
    int bigMissingNum = (num1 > num2) ? num1 : num2;

    cout << "The numbers to be missed are " << smallMissingNum << " and " << bigMissingNum << endl;
    for(int i = 0; i < smallMissingNum; ++i){
        vec.push_back(i);
    }

    for(int i = smallMissingNum + 1; i < bigMissingNum;  ++i){
        vec.push_back(i);
    }

    for(int i = bigMissingNum + 1; i < 100; ++i){
        vec.push_back(i);
    }

    random_shuffle(vec.begin(), vec.end());

    for(int i = 0; i < 98; ++i){
        cout << setw(4) << vec.at(i);
        if((i + 1) % 10 == 0){
            cout << endl;
        }
    }
    cout << endl;

    for(vector<int>::iterator it = vec.begin(); it != vec.end(); ++it){
        *arr ++ = *it;
    }
}

void FindMissingNumbers1(int* arr, int& num1, int& num2)
{
    int sum = 0;
    int squareSum = 0;
    for(int i = 0; i < 98; ++i){
        sum += arr[i];
        squareSum += arr[i] * arr[i];
    }

    int expectedSum = (0 + 99) * 100 / 2;
    int expectedSquareSum = 99 * (99 + 1) * (2 * 99 + 1) / 6;

    int aPlusB = expectedSum - sum;
    int asqPlusBsq = expectedSquareSum - squareSum;
    int aMulB = (aPlusB * aPlusB - asqPlusBsq) / 2;

    int t = sqrt((double)aPlusB * aPlusB - 4 * aMulB);
    num1 = (int)(aPlusB - t) / 2;
    num2 = (int)(aPlusB + t) / 2;

}

int main(int argc, char* argv[])
{
    srand(1);

    for(int i = 0; i < 10; i++){
        int arr[98];
        InitArray(arr);
        int num1, num2;
        FindMissingNumbers1(arr, num1, num2);
        cout << "The missing numbers are : " << endl;
        cout << "Num 1 : " << num1 << endl;
        cout << "Num 2 : " << num2 << endl << endl;
    }

    return 0;
}

Output
The numbers to be missed are 41 and 67
   6  28  51  53  69  71  76  93  49  20
   0  88  95  10   3  22   7  77  43  31
   1  32   2  39  58  11  64  82  83  48
  12  40  75  73  90  68  86  98  79  91
  14  34   8  36  62  24  17  61  21  45
  94  27  47  70  33  57  92  72  44  35
  50  78  15  96  26  42  97  23  56  65
  13  55  59  66  52  29  74  99  19  30
  85  84  25  38  89  60  37  63  16   4
  46  54  18   9  81  87  80   5
The missing numbers are :
Num 1 : 41
Num 2 : 67

The numbers to be missed are 15 and 33
  36  71  10  60  57   2  97  84  75  47
  61  24  79  32  73  62  12  50  56   5
  87  98  90   6   8  16  35  40  59  49
  18   9   0  43  63  31  82  20  91  29
  25  95  85  17  65  54  96  88  66  27
  53  55  22  83  21   3  46  42  86  34
  92   4  70  58  77  52  69  26  64  19
  94   7  51  80  68  41  72  99  11  39
  38   1  67  30  93  44  48  89  78  28
  14  37  74  81  45  23  76  13
The missing numbers are :
Num 1 : 15
Num 2 : 33

The numbers to be missed are 9 and 14
   4  45   2  23  68   5  74  72  65  36
  83  51  41  40  79  20  70  33  12  69
  97  55  57  26  39  91  31  47  58  71
  64   8   3  56  53  52  73  35  84  28
  86  82  19  88  37  85  50  96  15  42
  76  54  11  61  93   1  48  17  25  78
  49  92   7  90  24  63  94  30  80  95
  13  32  77  44  10  27  21  59  18  43
  66  29  46  87  62  22  75   6   0  89
  60  67  16  81  38  98  99  34
The missing numbers are :
Num 1 : 9
Num 2 : 14

The numbers to be missed are 21 and 89
  29  65  71  23  41  81  36  19  80  82
   4  34  69  50  12   9  31  62  73  96
  94  28  49  13  75  97  95  22  83  42
   1  10  66  70   0   6  35  98  76  99
  57   7  55  26  91  47  11  32  54  60
   2  44  39  90  86  52  79   8  87  33
  40  43  46  18  53  20  84  59  51  56
  27  92  72  37  77  63  25  88  93  48
   5  85  78  24  74  58   3  45  14  68
  67  38  16  61  17  15  64  30
The missing numbers are :
Num 1 : 21
Num 2 : 89

The numbers to be missed are 18 and 97
  47  15  14  19  92  42   8  35  36   6
  40  55  64  22  67   7  17  43  66  54
  44  74  56  76  29  82  98  70  77  57
  41  73  68  71  93  79  81  63  94   0
  69  78  88  65  89  16  24  86  31  25
  21  50  39   1  72  48  33  37  11   4
  49  59  95  34  51  27   3  90   2  13
  85  28  30  46  20   9  58  45  91  10
  83  62  96  26  87  32  75  61  52  23
  53  38  84  80   5  12  99  60
The missing numbers are :
Num 1 : 18
Num 2 : 97

The numbers to be missed are 37 and 61
  19  66  17  11  41   3  60  71  74   1
  31  50  23  96  15  68   8  43  45  42
  34  46  82  49  62  65  53  77  78  59
  72  14  63  40   4  55  21  91  39  10
  32  85  51  67  69  73  36  97   2  79
  70  16   9  13  89   0  56  54  87  22
  58  25  90  35  64  52  28  88  38  95
  47  99  18  20  83   7  44   6  26  81
  94  75  92  76  93  84   5  33  80  29
  12  24  98  57  48  86  30  27
The missing numbers are :
Num 1 : 37
Num 2 : 61

The numbers to be missed are 48 and 75
  70  47  68   8  22  77  94  56  78  30
  53  18  92  79  23  61  16  76  29  12
   2  91  50  45  37  32  83  74  60  97
  63  66  42  85  95  89  44  39  52  98
  24  28  82  73  15   6  21  27  14   4
  25  58  93  11  43  65  62  67  87  99
  35  41  10  80  55   5  38  57   3  81
  88   0  36  19  51   9  84  20  46  13
  49  96   1   7  59  26  17  33  54  69
  31  34  40  71  72  64  86  90
The missing numbers are :
Num 1 : 48
Num 2 : 75

The numbers to be missed are 13 and 92
  27  64  97  84  29  46  20  36  31  82
  70  21  66  83  71  87  12  67  42  77
  68  73  37  58  63  60   5   7  80  14
  47  17  23  98  99  79  26  38   9  15
  69  57  18  10  45  35  62  49  96  86
  22  53  30  89  25  24  93  85  91  33
  32  50  19  52  40  51  43  74  94  61
  59  95  72   2  34  65  11  48  81   4
   8  56  41  28  44   6  55   1  78   3
  90  54   0  16  76  75  88  39
The missing numbers are :
Num 1 : 13
Num 2 : 92

The numbers to be missed are 49 and 90
  69   5  84   4  47  27  19   7  40  43
  77  31  32   3  55  30  93  76  75  67
  52  66  11  71  95  79  74  10  16  42
  65  24  41   0  21  89  88   2  96  81
  58  73  82  64  15   9   8  99  57  20
  39  48   1  62  23  53  70  46  97  51
  44  87  80  50  38  35  86  12   6  13
  18  22  59  91  33  25  36  63  34  37
  68  28  92  56  14  72  45  78  83  17
  54  98  29  26  85  60  94  61
The missing numbers are :
Num 1 : 49
Num 2 : 90

The numbers to be missed are 61 and 64
  48  83  20  47   5  59  37  58  97  30
  87  51  77  67  91  63  90  93  36  17
  11  95  10  70  86  28  26  69  85  19
  71  60  84  73  18  32  46  24   4  39
   2   3  68  35  16  96  31  56  50  14
   8  22  44  52  80  89  62  76  74  34
  65  81  40  33  13  21  88  98   1  27
  92  43  99   7  25  45  82  41  72  79
  66  75  94  55  53  57  12  42  38  54
   0  49   6   9  78  29  23  15
The missing numbers are :
Num 1 : 61
Num 2 : 64

Press any key to continue . . .


  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值