Missing number in array | Practice | GeeksforGeeks

Given an array of size N-1 such that it only contains distinct integers in the range of 1 to N. Find the missing element.
Example 1:
Input:
N = 5
A[] = {1,2,3,5}
Output: 4

Example 2:
Input:
N = 10
A[] = {6,1,2,8,3,4,7,10,5}
Output: 9

Your Task :
You don't need to read input or print anything. Complete the function MissingNumber() that takes array and N as input  parameters and returns the value of the missing number.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 106
1 ≤ A[i] ≤ 106
0
rinnivatsa09in 6 hours
class Solution {   int MissingNumber(int array[], int n) {       // Your Code Here                    int actual = (n*(n+1)/2);             int total = 0;               for(int num : array){                   total += num;
               }                     return actual- total;
                }}
0
subhamksingh604in 4 hours
class Solution:
    def MissingNumber(self,array,n):
        return int(n*(n+1)/2-sum(array))
0
manikantrai2000in 3 hours
 int MissingNumber(vector<int>& array, int n) {       // Your code goes here       int a =0;       return (n * (n+1))/2 - accumulate(array.begin(),array.end(),a);   }
0
ashi111000011120 hours ago
 int MissingNumber(vector<int>& array, int n) {       // Your code goes here       int ans=0;       int sum =0 , sum_arr=0;       sum = n*(n+1)/2;       for(int i=0;i<array.size();i++){           sum_arr=sum_arr+array[i];                      ans=sum-sum_arr;       }           return ans;       }
0
anilchaudharry20 hours ago
JAVA Solution
 
class Solution {   int MissingNumber(int array[], int n) {       // Your Code Here       int tempSum = 0;       int actualSum = (n*(n+1))/2;       for(int i : array){           tempSum += i;       }       return actualSum - tempSum;   }}
0
zarvis23 hours ago
class Solution
{
  public:
    int MissingNumber(vector<int>& array, int n) 
    {
        int sum=0,sum_arr=0;
        sum=n*(n+1)/2;
        for(int i=0;i<array.size();i++)
             sum_arr=sum_arr+array[i];
            
        int miss_no=sum-sum_arr;
        return miss_no;
    }
};
0
mrjay95612 days ago
//Sorted the array first and then compared index with the value
class Solution {   int MissingNumber(int array[], int n) {              Arrays.sort(array);       int temp=0;       temp++;       for(int i=0;i<n-1;i++)       {           if(array[i]==temp)           {             temp=temp+1;           }           else           {              return temp ;            }       }       return temp;   }}
 
0
raoviveks593 days ago
//Sorted the Array first and then compared the indexes with the value
class Solution {   int MissingNumber(int array[], int n) {              Arrays.sort(array);       int temp=0;       temp++;       for(int i=0;i<n-1;i++)       {           if(array[i]==temp)           {             temp=temp+1;           }           else           {              return temp ;            }       }       return temp;   }}
0
dangwalshubh93 days ago
python soln. 
 
class Solution:   def MissingNumber(self,arr,n):       # code here       s = n*(n+1)/2       su = sum(arr)       return int(s-su) 
0
amitpalav113 days ago
array
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.