Java Code : Using the formula Sum = (n*(n+1))/2. Time O(n).


  • 0
    V
    class Solution {
        public int missingNumber(int[] nums) 
        {
                int n = nums.length;
        		int sum = (n*(n+1))/2;    		
        		for (int i =0;i<nums.length;i++)
        		{
        			sum = sum-nums[i];
        		}
        		
            return sum;
        }
    }
    

Log in to reply
 

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