C program to find the missing number in a given integer array of 1 to n

In this blog post, we learn how to write a C program to find the missing number in a given integer array of 1 to n? So here we will write the C program to find the most missing number in an array. We will also see how to display the missing number in an array using C programming.

Suppose you have an integers array of size N and the integers array contains N-1 integers and these integers are in the range of 1 to n. There are no duplicates in the list. One of the integers is missing from the list. So here our task to write an efficient C code to find the missing integer.

Example,

//An integer array with size 8 and contains element between 
1 to 8 and there is no duplicate element
Input: int arr[8] = {1, 2, 4, 6, 3, 7, 8};

//Missign element of the array
Output: 5;

Explanation: The missing number from 1 to 8 is 5

 

So let’s see the logic to find the missing number in a given integer array of 1 to N. Suppose arr is an integer array of size N (arr[N] ) and contains the elements between 1 to N-1.

1. We know that the addition of a series from 1 to N is the N*(N+1)/2. We will store it in a variable name ‘completeSum’.

//Sum from 1 to N

const in completeSum = (N*(N+1))/2;

2. We will store the sum of array elements in a variable ‘arrSum’.

3. Now we will subtract arrSum from the completeSum to get the missing number.

//Now get the missing number 

const int missigngNum = completeSum - arrSum ;

 

 

C program to find the missing number in a given integer array of 1 to n:

In the below program, I have taken array size 8. You can change the size according to your choice.

#include<stdio.h>
#include <stdlib.h>


#define ARRAY_SIZE  8


// Find the missing number in the given array
int getMissingNumber(int arr[], int n)
{
    int i = 0;
    // get sum of integers between 1 to n
    const int completeSum  = n*(n + 1)/2;

    //Store array element sum
    int arrSum = 0;

    for (i= 0; i < n; i++)
    {
        arrSum += arr[i];
    }
    // Missing number
    return (completeSum - arrSum);
}


int main()
{
    int arr[ARRAY_SIZE] = {1, 2, 4, 6, 3, 7, 8};

    const int missingNum = getMissingNumber(arr, ARRAY_SIZE);

    // print the most occurring numbers
    printf("Missing Number = %d\n", missingNum);

    return 0;
}

Output:

missing number in array in C

 

If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.

Your free trial is waiting

 

Using the XOR operation we can also find the one missing number from the array which contains elements from 1 to N-1. Here we will use the property of XOR (A ˆ A = 0) to get a solution without worrying about the problem of bit overflow. And also XOR is both safer and faster than summation.

#include<stdio.h>
#include <stdlib.h>


#define ARRAY_SIZE  6


// Find the missing number in the given array
int getMissingNumber(int arr[], int n)
{
    int i = 0;
    int missigngNum  = 0;
    for( i=0; i<n; i++)
    {
        missigngNum  ^= arr[i];
        missigngNum  ^= (i + 1);
    }
    return missigngNum ;
}


int main()
{
    int arr[ARRAY_SIZE] = {2,3,1,5,4};

    const int missingNum = getMissingNumber(arr, ARRAY_SIZE);

    // print the most occurring numbers
    printf("Missing Number = %d\n", missingNum);

    return 0;
}

Output:

array missing element in C

 

Recommended Articles for you: