Easy Java Solution in O(n) runtime and O(1) complexity with no XOR etc.


  • 0
    R

    Calculate the sum of first n integers i.e. n*(n+1)/2 and calculate the sum of integers in the array and subtract sum1 - sum2

    {

    public class Solution {

    public int missingNumber(int[] nums) {
        
        if(nums.length == 0)
            return 0;
        int n = nums.length;
    
        int sum = n*(n+1)/2;
        int sum2=0;
        for(int i : nums){
            sum2 +=i;
        }
        return sum-sum2;
    } }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.