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
ianuragin 3 hours
Using Bit Manipulation
int MissingNumber(vector<int>& array, int n) {
        // Your code goes here
        int xr=0;
        for(auto i: array){
            xr^=i;
        }
        for(int i=1;i<=n;i++){
            xr^=i;
        }
        return xr;
    }
+1
20102067amitnarain 2 hours
Java Code
class Solution {   int MissingNumber(int array[], int n) {       int total_sum = (n * (n + 1)) / 2;       for (int i = 0; i < n-1; i++) {   // as the value of n is 5 but the //total elements are 4 in the array so the loop will run from 0 to 3
// simply you can use array.length           total_sum = total_sum - array[i];       }       return total_sum;          }}
0
anshu_sharma_4 hours ago
class Solution {    int MissingNumber(int array[], int n) {        // Your Code Here               int sum=0;        int Total_sum=n*(n+1)/2;        for(int i=0;i<array.length;i++){            sum=sum+array[i];        }        return (Total_sum-sum);    }}
0
wajidhossain19 hours ago
CPP Solution: 
int MissingNumber(vector<int>& array, int n) {       int missing_number;       int sum2= 0;       int sum = n*(n+1)/2;       for(int i =0;i<array.size();i++)       {           sum2+=array[i];       }       return missing_number = sum-sum2;   }
+1
arunkhw19981 day ago
class Solution:   def MissingNumber(self,array,n):       s = n*(n+1)//2       for i in array:           s -= i
       return s
0
prateekgoyal21 day ago
How do we think and reach the solution? Explained simply here:- 
 
 
0
swatidubey9142 days ago
Best solution in java
 
class Solution {   int MissingNumber(int array[], int n) {              //The formula of the sum of first n natural numbers is S=n(n+1)/2 .              //No_ele = total_no_of_elements_expected       //total_sum = sum_total_no_of_elements_expected              int No_ele = n; // n = array.length + 1       int total_sum = No_ele *(No_ele + 1)/2;              int sum = 0;       for(int i = 0 ; i < array.length ; i++){           sum = sum + array[i];       }       return (total_sum - sum);   }}
0
dsouzapraveen242 days ago
total = n * (n + 1)/2 arraySum = sum(array) #len(arr) - 1return round(total - arraySum)
0
dsouzapraveen24
This comment was deleted.
0
rahuldutta3872 days ago
//why my code is not running for multiple testcases whats wrong with this ... it is running for 200 testcases but for 40 it is showing wrong answer.//
class Solution{ public:   int MissingNumber(vector<int>& array, int n) {       // Your code goes here       unordered_set<int> s;       for(int i=0;i<n;i++){           s.insert(array[i]);       }       for(int i=1;i<=n;i++){           if(s.find(i)==s.end()){               return i;           }       }       return 0;   }};
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.