Missing number | Practice | GeeksforGeeks

Vaibhav likes to play with numbers and he has N numbers. One day he was placing the numbers on the playing board just to count that how many numbers he has. He was placing the numbers in increasing order i.e. from 1 to N. But when he was putting the numbers back into his bag, some numbers fell down onto the floor. He picked up all the numbers but one number, he couldn't find. Now he has to go somewhere urgently, so he asks you to find the missing number.
NOTE: Don't use Sorting


Example 1:
Input:                                                       
N = 4                                        
A[] = {1, 4, 3}
Output:
2     
Explanation:
Vaibhav placed 4 integers but he picked
up only 3 numbers. So missing number
will be 2 as it will become 1,2,3,4.
 
Example 2:
Input:                        
N = 5
A[] = {2, 5, 3, 1}
Output:
4
Explanation:
Vaibhav placed 5 integers on the board,
but picked up only 4 integers, so the
missing number will be 4 so that it
will become 1,2,3,4,5.
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function missingNumber() which takes the array A[] and its size N as inputs and returns the missing number.
 
Exected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
2 ≤ N ≤ 104
1 ≤ A[i] ≤ 104
Size of the array A[] = N-1 
0
harshilrpanchal19986 days ago
java solution
 
Arrays.sort(arr);int sum=0, result =0;for (int i=0 ; i < arr.length ; i++){    sum += arr[i];}for (int i=1 ; i <= N ; i++ ){    result += i;}return result-sum;
0
kssaini32011 week ago
C++  0.31/1.6
 
int missingNumber(int A[], int N){   // Your code goes here   int sum = (N*(N+1))/2;   int add=0;   for(int i=0;i<N-1;i++)   {       add += A[i];   }   return sum-add;}
0
kerim22 weeks ago
int missingNumber(int A[], int N){
   // Your code goes here
   int totalSum = N*(N+1)/2;
   for(int i = 0; i < N-1; i++) totalSum -= A[i];
   return totalSum;
}
0
mehraparthak02 weeks ago
int missingNumber(int arr[], int N){   int sum=(N*(N+1))/2;   for(int i=0;i<N-1;i++){       sum-=arr[i];   }   return sum;}
0
arvind16yadav4 weeks ago
/*JAVA CODE*/
 public static int missingNumber(int A[], int N)   {        // Your code goes here        //find sum of given numbers and subtract from sigma n                int sum_of_n_numbers = N*(N+1)/2;        int missing_number = 0;        int given_sum = 0;        for(int x : A){            given_sum += x;        }        missing_number = sum_of_n_numbers - given_sum;                return missing_number;   }
0
mail2rajab011 month ago
// Simple Java Solution
Total Time Taken: 0.6/4.5
 
class Compute {      public static int missingNumber(int A[], int N)   {        // Your code goes here        int sum=N*(N+1)/2;        for(int i=0;i<N;i++){            sum -=A[i];        }        return sum;   }}
0
soumyarupchatterjee1111 month ago
#Simple Java Solution 
class Compute {      public static int missingNumber(int A[], int N)   {        // Your code goes here        int sum=0;        for(int i=1;i<=N;i++)        {            sum+=i;        }        for(int i=0;i<A.length;i++)        {            sum=sum-A[i];        }        return sum;   }}
+1
mukulvyasg1 month ago
python
 
def missingNumber( A, N):
    # Your code goes here
    return sum(list(range(N+1)))-sum(A)
0
hasnainraza1998hr2 months ago
C++, 0.3
=============
int missingNumber(int A[], int N){   int sum = N*(N+1)/2;   for(int i=0;i<N-1;i++){       sum -= A[i];   }   return sum;}
0
praveenkumar647672 months ago
   int count = 0;              int total = (N*(N+1))/2;              for(int i=0; i<N-1; i++){            count += A[i];       }              return total - count;
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.