Get best answers to any doubt/query/question related to programming , jobs, gate, internships and tech-companies. Feel free to ask a question and you will receive the best advice/suggestion related to anything you ask about software-engineering , development and programming problems .

0 like 0 dislike
4,013 views
in Online Assessments by Expert (44,360 points)

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 (44,360 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 (44,360 points)
...