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 ≤ 10^{4}
1 ≤ A[i] ≤ 10^{4}
Size of the array A[] = N-1

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Mathematics

Starter code: #User function Template for python3



def missingNumber( A, N):

     # Your code goes here

Solutions:
def missingNumber(A, N):
	nums = set([n for n in range(1, N + 1)])
	d = list(nums.difference(A))
	return d[0]


def missingNumber(A, N):
	a = N * (N + 1) // 2
	b = sum(A)
	return a - b


def missingNumber(A, N):
	a = list(range(1, N + 1))
	b = N * (N + 1) // 2
	c = sum(A)
	return b - c


def missingNumber(A, N):
	totalSum = N * (N + 1) / 2
	currSum = 0
	for i in A:
		currSum += i
	return int(totalSum - currSum)


def missingNumber(A, N):
	n = N + 1
	sum = n * (n - 1) / 2
	for i in A:
		sum -= i
	return int(sum)


def missingNumber(arr, n):
	total_sum = n * (n + 1) // 2
	arr_sum = sum(arr)
	mis_num = total_sum - arr_sum
	return mis_num


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


def missingNumber(A, N):
	total = N * (N + 1) // 2
	sum_arr = sum(A)
	return total - sum_arr


def missingNumber(A, N):
	act_sum = N * (N + 1) // 2
	got_sum = sum(A)
	return act_sum - got_sum


def missingNumber(A, N):
	s2 = N * (N + 1) / 2
	s3 = sum(A)
	return int(s2 - s3)


def missingNumber(A, N):
	s1 = N * (N + 1) / 2
	s2 = sum(A)
	return int(s1 - s2)


def missingNumber(A, N):
	return N * (N + 1) // 2 - sum(A)


def missingNumber(A, N):
	total = N * (N + 1) / 2
	my_sum = sum(A)
	return int(total - my_sum)


def missingNumber(A, N):
	sumtotal = N * (N + 1) // 2
	sumNumber = 0
	for i in A:
		sumNumber = sumNumber + i
	return sumtotal - sumNumber


def missingNumber(A, N):
	set_sum = sum(A)
	correct_sum = N * (N + 1) // 2
	return correct_sum - set_sum


def missingNumber(A, N):
	A = sorted(set(A))
	for i in range(N - 1):
		if A[i] != i + 1:
			return i + 1
	return N


def missingNumber(A, N):
	A.sort()
	for i in range(N - 1):
		if A[i] != i + 1:
			return i + 1
	return N


def missingNumber(A, N):
	s = N * (N + 1) / 2
	su = 0
	for i in A:
		su = su + i
	return int(s - su)


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


def missingNumber(A, N):
	sum1 = N * (N + 1) // 2
	sum2 = sum(A)
	return sum1 - sum2


def missingNumber(A, N):
	d = {}
	for i in range(len(A)):
		try:
			d[A[i]] = 0
		except:
			d[A[i]] += 1
	for i in range(1, N + 1):
		if i not in d:
			return i
	return N


def missingNumber(A, N):
	s = N * (N + 1) // 2
	total = sum(A)
	return s - total


def missingNumber(A, N):
	A.sort()
	ans = 0
	for i in range(N):
		if i < len(A):
			if 1 + i != A[i]:
				ans = i + 1
				break
		else:
			ans = A[0] + i
	return ans


def missingNumber(A, N):
	sum1 = N * (N + 1) / 2
	sum2 = 0
	for i in A:
		sum2 = sum2 + i
	return int(sum1 - sum2)


def missingNumber(A, N):
	sum = 0
	for i in A:
		sum += i
	return N * (N + 1) // 2 - sum


def missingNumber(A, N):
	su = 0
	for i in range(1, N + 1):
		su = su + i
	su1 = sum(A)
	return su - su1


def missingNumber(A, N):
	s = set(A)
	for i in range(1, N + 1):
		if i not in s:
			return i


def missingNumber(A, N):
	x1 = A[0]
	x2 = 1
	for i in range(1, N - 1):
		x1 ^= A[i]
	for i in range(2, N + 1):
		x2 ^= i
	return x1 ^ x2


def missingNumber(A, N):
	summ = sum(A)
	data = N * ((N + 1) / 2)
	return int(data - summ)


def missingNumber(A, N):
	m1 = N * (N + 1) / 2
	m2 = sum(A)
	return int(m1 - m2)


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


def missingNumber(A, N):
	return abs(sum(A) - sum(range(N + 1)))


def missingNumber(arr, n):
	n = len(arr)
	m = n + 1
	total = m * (m + 1) // 2
	return total - sum(arr)


def missingNumber(A, N):
	s1 = N * (N + 1) // 2
	s2 = 0
	for ele in A:
		s2 += ele
	return s1 - s2


def missingNumber(A, N):
	actual_sum = N * (N + 1) // 2
	array_sum = 0
	for i in range(N - 1):
		array_sum = array_sum + A[i]
	difference = actual_sum - array_sum
	return difference


def missingNumber(a, n):
	sum = 0
	for x in a:
		sum = sum + x
	total = n * (n + 1) / 2
	return int(total - sum)


def missingNumber(A, n):
	s = 0
	for i in range(n - 1):
		s = s + A[i]
	sum = n * (n + 1) // 2
	return sum - s


def missingNumber(A, N):
	temp = (N * N + N) // 2
	t = sum(A)
	return temp - t


def missingNumber(A, N):
	nu = N * (N + 1) // 2
	y = sum(A)
	return nu - y


def missingNumber(A, N):
	sum = curr = 0
	for i in range(1, N + 1):
		sum += i
	for i in A:
		curr += i
	return sum - curr


def missingNumber(A, N):
	sums = sum(A)
	ans = N * (N + 1) // 2 - sums
	return ans


def missingNumber(A, N):
	sum1 = 0
	for i in A:
		sum1 += i
	sum2 = 0
	for i in range(1, N + 1):
		sum2 += i
	return sum2 - sum1


def missingNumber(A, N):
	k = sum(A)
	s = N * (N + 1) // 2
	return s - k


def missingNumber(A, N):
	sum1 = N * (N + 1) / 2
	sum2 = sum(A)
	num = int(sum1 - sum2)
	return num


def missingNumber(A, N):
	A.sort()
	if N not in A:
		return N
	for i in range(N):
		if i + 1 == A[i]:
			continue
		else:
			return i + 1


def missingNumber(A, N):
	hashmap = {}
	for i in A:
		hashmap[i] = 1 + hashmap.get(i, 0)
	for i in range(1, N + 1):
		if i not in hashmap:
			return i


def missingNumber(A, N):
	XOR = 0
	for i in range(1, N + 1):
		XOR = XOR ^ i
	for num in range(0, N - 1):
		XOR = XOR ^ A[num]
	return XOR


def missingNumber(A, N):
	s = 0
	for i in range(1, N + 1):
		s = s + i
	for i in A:
		s = s - i
	return s


def missingNumber(A, N):
	lst = [i for i in range(1, N + 1)]
	set1 = set(A)
	set2 = set(lst)
	a = list(set2 - set1)
	return a[0]


def missingNumber(A, N):
	a = sum(A)
	total = N * (N + 1) // 2
	ans = total - a
	return ans


def missingNumber(A, N):
	sum = 0
	for ele in A:
		sum += ele
	return N * (N + 1) // 2 - sum


def missingNumber(A, N):
	s = N * (N + 1) // 2
	if len(A) != max(A):
		return s - sum(A)
	else:
		return max(A) + 1


def missingNumber(A, N):
	sum_ = 0
	for number in A:
		sum_ += number
	return N * (N + 1) // 2 - sum_


def missingNumber(A, N):
	sumAll = sum(list(range(N + 1)))
	sumMissing = sum(A)
	return abs(sumAll - sumMissing)


def missingNumber(A, N):
	sumA = sum(A)
	actual = N * (N + 1) // 2
	return actual - sumA


def missingNumber(A, N):
	lst = [i for i in range(1, N + 1)]
	while A:
		x = A.pop()
		lst[x - 1] = 0
	for i in lst:
		if i != 0:
			return i
	return 0


def missingNumber(A, N):
	res = N * (N + 1) // 2
	temp = sum(A)
	missing = res - temp
	return missing


def missingNumber(A, N):
	totalSum = N * (N + 1) // 2
	arraySum = sum(A)
	missingNumber = totalSum - arraySum
	return missingNumber


def missingNumber(A, N):
	b = set(A)
	c = [i for i in range(1, N + 1)]
	d = set(c)
	e = d.difference(b)
	for j in e:
		return j


def missingNumber(A, N):
	ans = 0
	for i in range(1, N + 1):
		ans = ans ^ i
	for i in A:
		ans = ans ^ i
	return ans


def missingNumber(A, N):
	t_sum = N * (N + 1) // 2
	arr_sum = 0
	for i in range(len(A)):
		arr_sum += A[i]
	return t_sum - arr_sum


def missingNumber(A, N):
	missing_number = 0
	for i in range(1, N + 1):
		missing_number = missing_number ^ i
	for i in range(N - 1):
		missing_number = missing_number ^ A[i]
	return missing_number


def missingNumber(A, N):
	expected_sum = N * (N + 1) // 2
	actual_sum = sum(A)
	return expected_sum - actual_sum


def missingNumber(A, N):
	sxor = 0
	for el in range(N + 1):
		sxor ^= el
	for el in A:
		sxor ^= el
	return sxor


def missingNumber(A, N):
	s = int(N * (N + 1) / 2)
	su = sum(A)
	return s - su


def missingNumber(A, N):
	sum = N * (N + 1) / 2
	n = 0
	for i in range(N - 1):
		n = n + A[i]
	miss = sum - n
	return round(miss)


def missingNumber(A, N):
	sum_N = N * (N + 1) // 2
	sum_A = sum(A)
	missing_num = sum_N - sum_A
	return missing_num