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:
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:
Recommended Articles for you:
- Best gift for programmers.
- Best electronic kits for programmers.
- C program to find the most popular element in an array
- C program to find even occurring elements in an array of limited range
- Find sum of all sub-array of a given array.
- C program to segregate even and odd numbers
- Find an element in array such that sum of left array is equal to sum of right array.
- C Program to find the count of even and odd elements in the array.
- Write C program to find the sum of array elements.
- C program to find odd occurring elements in an array of limited range
- Find the sum of array elements using recursion
- C Program to reverse the elements of an array
- C Program to find the maximum and minimum element in the array
- Calculate size of an array in without using sizeof in C
- How to create a dynamic array in C?
- How to access 2d array in C?
- Dangling, Void, Null and Wild Pointers
- Function pointer in c, a detailed guide
- How to use the structure of function pointer in c language?
- Memory Layout in C.
- 100 C interview Questions
- File handling in C.