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 ≤ 10^{6}
1 ≤ A[i] ≤ 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Complete search

Starter code: #User function Template for python3





class Solution:

    def missingNumber(self,array,n):

        # code here

Solutions:
class Solution:

	def missingNumber(self, array, n):
		sum = 0
		for x in range(n - 1):
			sum = sum + array[x]
		num = n
		total = num * (num + 1) / 2
		return int(total - sum)


class Solution:

	def missingNumber(self, array, n):
		a = set(array)
		for x in range(1, n + 1):
			if x not in a:
				return x


class Solution:

	def missingNumber(self, array, n):
		res = n * (n + 1) // 2 - sum(array)
		return res


class Solution:

	def missingNumber(self, array, n):
		total_sum = n * (n + 1) // 2
		array_sum = sum(array)
		missing_element = total_sum - array_sum
		return missing_element


class Solution:

	def missingNumber(self, array, n):
		array_value = sum(array)
		a = n * (n + 1) // 2
		b = a - array_value
		return b


class Solution:

	def missingNumber(self, array, n):
		ts = sum(array)
		return n * (n + 1) // 2 - ts


class Solution:

	def missingNumber(self, arr, n):
		dict1 = {}
		for i in range(1, n + 1):
			dict1[i] = 0
		for i in range(n - 1):
			dict1[arr[i]] += 1
		for i in range(1, n + 1):
			if dict1[i] == 0:
				return i


class Solution:

	def missingNumber(self, array, n):
		total = n * (n + 1) // 2
		arr_sum = sum(array)
		return total - arr_sum


class Solution:

	def missingNumber(self, array, n):
		sum1 = 0
		sum2 = 0
		for i in range(0, n + 1):
			sum1 = sum1 + i
		for i in range(0, n - 1):
			sum2 = sum2 + array[i]
		return sum1 - sum2


class Solution:

	def missingNumber(self, array, n):
		array.sort()
		if array[-1] != n:
			return n
		else:
			for i in range(1, 1 + n):
				if i != array[i - 1]:
					return i


class Solution:

	def missingNumber(self, array, n):
		sum_N = n * (n + 1) // 2
		sum_arr = sum(array)
		missing_element = sum_N - sum_arr
		return missing_element


class Solution:

	def missingNumber(self, array, n):
		array.sort()
		i = 1
		while i <= n:
			for j in range(0, n - 1):
				if array[j] != i:
					return i
				else:
					i = i + 1
					continue


class Solution:

	def missingNumber(self, array, n):
		array_sum = 0
		for i in range(n - 1):
			array_sum = array_sum + array[i]
		sum_of_n_elements = int(n * (n + 1) / 2)
		missing_element = sum_of_n_elements - array_sum
		return missing_element


class Solution:

	def missingNumber(self, array, n):
		return int(n * (n + 1) / 2 - sum(array))


class Solution:

	def missingNumber(self, array, n):
		t = n * (n + 1) // 2
		s = sum(array)
		return t - s


class Solution:

	def missingNumber(self, array, n):
		actualSum = 0
		expectedSum = n * (n + 1) // 2
		for item in array:
			actualSum += item
		return expectedSum - actualSum


class Solution:

	def missingNumber(self, array, n):
		expected_sum = n * (n + 1) // 2
		actual_sum = sum(array)
		missing_number = expected_sum - actual_sum
		return missing_number


class Solution:

	def missingNumber(self, array, n):
		i = 1
		array_sum = sum(array)
		sum_list = 0
		while i <= n:
			sum_list = sum_list + i
			i = i + 1
		num_miss = sum_list - array_sum
		return num_miss


class Solution:

	def missingNumber(self, arr, N):
		total_sum = N * (N + 1) // 2
		array_sum = sum(arr)
		missing_number = total_sum - array_sum
		return missing_number


class Solution:

	def missingNumber(self, array, n):
		num_set = set(array)
		for i in range(1, n + 1):
			if i not in num_set:
				return i
		return 0


class Solution:

	def missingNumber(self, array, n):
		array_set = set(array)
		check = set(range(1, n + 1))
		missing = list(check - array_set)
		return missing[0]


class Solution:

	def missingNumber(self, array, n):
		totsum = n * (n + 1) / 2
		arrsum = 0
		for x in array:
			arrsum = arrsum + x
		miss = int(totsum - arrsum)
		return miss


class Solution:

	def missingNumber(self, array, n):
		ts = n * (n + 1) / 2
		for num in array:
			ts -= num
		return int(ts)


class Solution:

	def missingNumber(self, array, n):
		data = 0
		for i in array:
			data = data ^ i
		for i in range(1, n + 1):
			data = data ^ i
		return data


class Solution:

	def missingNumber(self, array, n):
		array.sort()
		a = sum(array)
		total_sum = n * (n + 1) // 2
		missing = total_sum - a
		return missing


class Solution:

	def missingNumber(self, array, n):
		l1 = []
		for i in range(1, n + 1):
			l1.append(i)
		x = set(l1).difference(array)
		return max(x)


class Solution:

	def missingNumber(self, array, n):
		xor = 0
		xor1 = 0
		for i in range(1, n + 1):
			xor = xor ^ i
		for i in range(n - 1):
			xor1 = xor1 ^ array[i]
		return xor ^ xor1


class Solution:

	def missingNumber(self, array, n):
		if n < 1:
			return
		tmp = [0] * n
		for i in range(n - 1):
			tmp[array[i] - 1] = 1
		for i in range(0, n):
			if tmp[i] == 0:
				return i + 1
		return


class Solution:

	def missingNumber(self, arr, n):
		arr.sort()
		c = 0
		if n - 1 == 1:
			if arr[0] > 1:
				return arr[0] - 1
			return arr[0] + 1
		for i in range(n - 2):
			if arr[i + 1] - arr[i] > 1:
				c = 1
				return arr[i] + 1
		if arr[n - 2] == n:
			return arr[0] - 1
		if c == 0:
			return arr[n - 2] + 1
		return 0


class Solution:

	def missingNumber(self, array, n):
		m = n * (n + 1) // 2
		return m - sum(array)


class Solution:

	def missingNumber(self, array, n):
		s = sum(array)
		d = n * (n + 1) // 2
		a = d - s
		return a


class Solution:

	def missingNumber(self, array, n):
		return sum(range(1, n + 1)) - sum(array)


class Solution:

	def missingNumber(self, array, n):
		l = sum(range(1, n + 1))
		a = sum(array)
		return l - a


class Solution:

	def missingNumber(self, array, n):
		l = [int(i) for i in range(1, n + 1)]
		a = set(array)
		l = set(l)
		return list(l.difference(a))[0]


class Solution:

	def missingNumber(self, array, n):
		sum = 0
		sum1 = 0
		for i in range(1, n + 1):
			sum = sum + i
		for i in range(len(array)):
			sum1 = sum1 + array[i]
		return sum - sum1


class Solution:

	def missingNumber(self, array, n):
		a = 0
		for i in range(1, n + 1):
			a += i
		b = 0
		for i in array:
			b += i
		return a - b


class Solution:

	def missingNumber(self, array, n):
		s = set()
		array = set(array)
		for i in range(1, n + 1):
			s.add(i)
		l = list(s - array)
		return l[0]


class Solution:

	def missingNumber(self, array, n):
		a = list(range(1, n + 1))
		b = sum(a)
		c = sum(array)
		k = b - c
		return k


class Solution:

	def missingNumber(self, array, n):
		dict1 = {}
		for val in array:
			dict1[val] = 0
		for val in range(1, n + 1):
			if val not in dict1:
				return val


class Solution:

	def missingNumber(self, array, n):
		a = set(range(1, n + 1))
		missing_ele = list(a - set(array))[0]
		return missing_ele


class Solution:

	def missingNumber(self, array, n):
		tot_sum = 0
		for i in range(1, n + 1):
			tot_sum += i
		sum_arr = sum(array)
		miss = tot_sum - sum_arr
		return miss


class Solution:

	def missingNumber(self, arr, n):
		s = set(arr)
		(i, l) = (1, n)
		while i <= l:
			if i not in s:
				return i
			if l not in s:
				return l
			i += 1
			l -= 1
		return -1


class Solution:

	def missingNumber(self, array, n):
		x = n * (n + 1) // 2
		y = sum(array)
		return x - y


class Solution:

	def missingNumber(self, array, n):
		sum_n = n * (n + 1) // 2
		s = 0
		for i in array:
			s = s + i
		return sum_n - s


class Solution:

	def missingNumber(self, array, n):
		array.sort()
		for i in range(n):
			if i + 1 > len(array) or i + 1 != array[i]:
				return i + 1


class Solution:

	def missingNumber(self, array, n):
		A = set(range(1, n + 1))
		missing_element = list(A - set(array))[0]
		return missing_element


class Solution:

	def missingNumber(self, array, n):
		res = 0
		for i in range(1, n + 1):
			res += i
		return res - sum(array)


class Solution:

	def missingNumber(self, array, n):
		counter = 1
		for i in sorted(array):
			if i != counter:
				return counter
			counter += 1
		return counter


class Solution:

	def missingNumber(self, array, n):
		summ = 0
		for i in range(1, n + 1):
			summ += i
		arr_sum = sum(array)
		return summ - arr_sum


class Solution:

	def missingNumber(self, array, n):
		total_sum = n * (n + 1) // 2
		sum = 0
		for x in array:
			sum += x
		return total_sum - sum


class Solution:

	def missingNumber(self, array, n):
		l = list(range(n + 1))
		tot = sum(l)
		arr = sum(array)
		return tot - arr


class Solution:

	def missingNumber(self, arr, n):
		k = 0
		p = 0
		for i in range(len(arr)):
			k += arr[i]
		for j in range(1, n + 1):
			p += j
		return p - k


class Solution:

	def missingNumber(self, array, n):
		k = n * (n + 1)
		sum = int(k / 2)
		s2 = 0
		for i in range(n - 1):
			s2 += array[i]
		return sum - s2


class Solution:

	def missingNumber(self, array, n):
		sum_N = n * (n + 1) // 2
		for num in array:
			sum_N -= num
		return sum_N


class Solution:

	def missingNumber(self, arr, n):
		chk = {i for i in range(1, n + 1)}
		for x in arr:
			chk.discard(x)
		return chk.pop()


class Solution:

	def missingNumber(self, array, n):
		a = n * (n + 1) / 2
		b = 0
		for i in array:
			b = b + i
		return int(a) - b


class Solution:

	def missingNumber(self, array, n):
		sumi = sum(list(range(1, n + 1)))
		diff = sumi - sum(array)
		return diff


class Solution:

	def missingNumber(self, array, n):
		sum = 0
		for i in array:
			sum = sum + i
		asum = n * (n + 1) / 2
		return int(asum - sum)