Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
2,819 views
in Online Assessments by Expert (46,090 points) | 2,819 views

2 Answers

0 like 0 dislike
Best answer

Question:
MEX Problem
Given an array arr contalning 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.

image
image

by Expert (46,090 points)
0 like 0 dislike

Solution:

 

private int solve(int[] A, int X){
    int[] reminder = new int[X];
    for (int n : A){
        reminder[n%X]++;
    }
    for (int i = 0; i < A.length; i++){
        if (--reminder[i%X]<0){
            return i;
        }
    }
    return A.length;
}
by Expert (46,090 points)