Atlassian | OA | maximum MEX
Anonymous User
2244

Atlassian OA

Given an array containing n non negative integers and an element x, In one operation, x can be added to or subtracted from any element of the array. MEX of an array is defined as the smallest non negative integer which is not present in the array. for example, the MEX of [0,1,1,3] is 2, and the MEX of [1,2,4] is 0

Find the maximum possible MEX of the array that can be achieved by doing the above operation any number of times

Explanation
arr = [0,1,2,1,3]
x = 3
if we add x to arr[1] we get arr = [0,4,2,1,3] having MEX equal to 5.
this is the maximum possible MEX that can be achieved

Constraints
1 <= n <= 10^5
0 <= arr[i] <= 10^9
1 <= x <= 10^5

testcase 0:
3 //value of n
1 3 4
2 //value of x
output:
2
explanation:
by subtracting x from arr[2] two times, arr = [1,3,0] that has MEX 2, the maximum possible MEX

testcase 1:
7
0 1 2 2 0 0 10
3
output:
7
explanation:
arr can be transformed into [0,1,2,5,3,6,4,3] that ha MEX=7, the maximum possible

Any approaches for this question are highly appreciated, Thank you!

Comments (4)