Complete-Preparation

🎉 One-stop destination for all your technical interview Preparation 🎉

View the Project on GitHub

268. Missing Number 🌟

Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.

O(NlogN) by Sorting

O(N) Time

class Solution{
public:
	int missingNumber(vector<int> &nums){
		int sum = 0, n = nums.size();
		for (int i = 0; i < n; i++){
			sum += nums[i];
		}
		return (n * (n + 1) / 2) - sum;
	}
};