C Program to Find Missing Numbers in an Array of Size N-1 with Numbers[1,N]

This is a C Program to identify the missing number in an integer array of size n-1 with numbers[1,N].

Problem Description

This C Program Identifies the Missing Number in an Integer Array of Size N-1 with Numbers[1,N].

Problem Solution

Take input from the user and performs bitwise operations as shown in the program below.

Program/Source Code

Here is source code of the C Program to To Identify the Missing Number in an Integer Array of Size N-1 with Numbers[1,N]. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program To Identify the Missing Number in an Integer 
 * Array of Size N-1 with Numbers[1,N]
 */
#include <stdio.h>
#define MAX 15
int missing_number_array(int [],int);
 
int main()
{
    int a[MAX], num, i, n;
 
    printf("enter the range of array\n");
    scanf("%d", &n);
    for (i = 0;i < n;i++)
    {
        printf("enter a[%d]element into the array:", i);
        scanf("%d", &a[i]);
    }
    num = missing_number_array(a, n);
    printf("The missing number -> %d\n", num);
}
 
/* To find the missing number in array */
int missing_number_array(int a[],  int n)
{
    int i;
    int s1 = 0; 
    int s2 = 0; 
 
    for (i = 0;i < n;i++)
        s1 = s1 ^ a[i];
    for (i = 1;i <= n + 1;i++)
        s2 = s2 ^ i; 
    return (s1 ^ s2);
}
Program Explanation

In this C Program, we are reading the range of array using ‘n’ variable. Using for loop we are entering the coefficient element values of an array. The missing_number_array() function is used to find the missing number in array.

advertisement
advertisement

Then ‘s1’ and ‘s2’ variables are used to compute the Binary XOR operation, copies the bit if it is set in one operand but not both. Again compute the Binary XOR Operation is performed for s1 and s2 variable value and returns the value. Print the missing number in an integer array of size N-1 with numbers [1, N].

Runtime Test Cases
 
$ cc bit29.c
$ a.out
enter the range of array
9
enter a[0]element into the array:1
enter a[1]element into the array:5
enter a[2]element into the array:2
enter a[3]element into the array:7
enter a[4]element into the array:3
enter a[5]element into the array:4
enter a[6]element into the array:10
enter a[7]element into the array:9
enter a[8]element into the array:6
The missing number -> 8
$ a.out
enter the range of array
4
enter a[0]element into the array:1
enter a[1]element into the array:5
enter a[2]element into the array:3
enter a[3]element into the array:2
The missing number -> 4
$ a.out
enter the range of array
4
enter a[0]element into the array:3
enter a[1]element into the array:2
enter a[2]element into the array:5
enter a[3]element into the array:4
The missing number -> 1

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.