Problem · Array
MediumAtlassian logoAtlassianFULLTIMEOA

Problem statement

Given an array arr of non-negative integers and a positive integer x, one operation adds x to or subtracts x from any array element. You may perform any number of operations.

The MEX is the smallest non-negative integer not present in an array. Return the maximum MEX that can be achieved.

Function

maximumMEX(arr: int[], x: int) → int

Examples

Example 1

arr = [0,1,2,1,3]x = 3return = 5

Add 3 to one occurrence of 1. The array can contain every value from 0 through 4, so its MEX is 5.

Example 2

arr = [1,3,4]x = 2return = 2

Subtract 2 twice from 4 to create 0. The value 2 cannot also be created from the remaining residues.

Constraints

  • 1 <= arr.length <= 100000
  • 0 <= arr[i] <= 10^9
  • 1 <= x <= 100000

More Atlassian problems

drafts saved locally
public int maximumMEX(int[] arr, int x) {
  // Write your code here.
}
arr[0,1,2,1,3]
x3
expected5
checking account