Skip to content
Home » C program to find the missing element in an array

C program to find the missing element in an array

Given an array of size N-1 such that it only contains distinct integers in the range of 1 to N. Below is the program to find the missing element in an array

#include <stdio.h>
int main() {
    int i,n;
    printf("Enter size of array: ");
    scanf("%d",&n);
    int arr[n-1];
    printf("Enter the elements of an array ");
    for(i=0;i<(n-1);i++){
        if
        scanf("%d",&arr[i]);
    }
    int exp_sum,act_sum,mis_ele;
    exp_sum=(n*(n+1)/2);
    act_sum=0;
    for(i=0;i<(n-1);i++){
        act_sum+=arr[i];
    }
    mis_ele=exp_sum-act_sum;
    printf("Missing element in the array is %d",mis_ele);
    return 0;
}

Output:

Enter size of array: 6
Enter the elements of an array 3
2
1
4
6
Missing element in the array is 5