'Finding missing numbers in an array

I am trying to create a code where given an ordered array with numbers between 1 and 10, the code returns all of the values missing.

My code is as follows:

int missingArray [] = {1, 3, 4, 5, 7, 8};

for (int i = 0; i < 11; i++) {
    if (missingArray[i] == i+1) {
        cout << "Continue. \n";
    }
    if (missingArray[i] != i+1) {
        cout << "The value of " << i+1 << " is missing. \n";
    }
}

I want the code to return Continue The value of 2 is missing Continue Continue Continue The value of 6 is missing Continue Continue The value of 9 is missing The value of 10 is missing

But instead, after I get the first "missing" element, it lists everything as missing. Anyone have any suggestions?



Solution 1:[1]

Your code leads to undefined behavior since missingArray[i] is not valid for values of i greater than 5.

You need to change your approach a little bit.

int missingArray [] = {1, 3, 4, 5, 7, 8};

int* start = missingArray;
int* end = start + sizeof(missingArray)/sizeof(*missingArray);

for (int i = 1; i < 11; i++)
{
   if ( std::find(start, end, i) == end )
   {
      cout << i << " is missing.\n";
   }

   // Optionally
   else
   {
      cout << "Found " << i << "\n";
   }
}

Solution 2:[2]

you check missingArray[i] == i+1

1 == 1

3 == 2

4 == 3

5 == 4

...

so after first condition 1==1 others are never equal.

int missingArray[] = { 1, 3, 4, 5, 7, 8 };
int k = 0;

for (int i = 0; i < 10; i++) {

    if (missingArray[k] == i + 1) {
        cout << "Continue. \n";
        k++;
    }
    else if (missingArray[k] != i + 1) {
        cout << "The value of " << i + 1 << " is missing. \n";
    }
}

Solution 3:[3]

My approach would be to select each element of the array in turn and then iterate between one greater than that value and the next element in the array.

Then to finish off iterate between the final value and the maximim value you are seeking (11 in this case).

    int missingArray [] = {1, 3, 4, 5, 7, 8};

    int j = 0;

    for(auto i = 0U; i < sizeof(missingArray)/sizeof(int) - 1; ++i)
        for(j = missingArray[i] + 1; j < missingArray[i + 1]; ++j)
            std::cout << "missing: " << j << '\n';

    for(++j; j < 11; ++j)
        std::cout << "missing: " << j << '\n';

Output:

missing: 2
missing: 6
missing: 9
missing: 10

Solution 4:[4]

As Pmar said, your initial assumption was not valid. I change the code a little bit. I hope this will help you.

 #include<stdio.h>
 #include <iostream>
 using namespace std;

 int main (){
 int missingArray [] = {1, 3, 4, 5, 7, 8};
 int numbers_mising = 0;

 for (int i = 0; i < 10; i++) {
     if (missingArray[i - numbers_mising] == i+1) {
        cout << "Continue. \n";
     }
     if (missingArray[i -  numbers_mising] != i+1) {
         cout << "The value of " << i+1 << " is missing. \n" << numbers_mising << "\n";
         numbers_mising++;
     }
   }
 }

In this example, also the number two is missing. You do not need to know in advance what numbers are missing with this solution. I use a variable to keep track of the numbers missing and changing the index of the array.

Solution 5:[5]

you can go with this logic also this is very simplest logic for you.

Expected Output:
The value of 3 is missing.
The value of 7 is missing.

int missingArray[]={1,2,4,5,6,8};

    int n=sizeof(missingArray)/sizeof(missingArray[0]);

    int i=0,k=1;
    while (i<n)
    {
        if(missingArray[i]==k)
        {
            i++;
            k++;
        }
        else 
        {
           cout<<"The value of "<<k<<" is missing. \n";
           k++;
        }
        
        
    } 

Solution 6:[6]

    int main()
{
    char array[10] = {1,2,3,4,5,6,7,7,9,10};
    char i;
    char i_2 = 1;
    char not_ok = 1;
    while(i_2 < 11){
        i = 0;
        while(i < 10){
            if(array[i] == i_2){
                not_ok = 0;
            }
            i++;
        }   
        if(not_ok){
            printf("Missing %d\n",i_2);
        }
        not_ok = 1;
        i_2++;
    }
    return 0;

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 R Sahu
Solution 2 user7343621
Solution 3 Galik
Solution 4 JBeloqui
Solution 5 MacTech. com
Solution 6 Black White