fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static int calMEX(int[] copy){
  11. HashSet<Integer> st = new HashSet<>();
  12. for(int i : copy) st.add(i);
  13.  
  14. for(int i = 0; i < copy.length; i++){
  15. if(!st.contains(i)) return i;
  16. }
  17. return copy.length;
  18. }
  19.  
  20. public static int[] maximumMEX(int[] nums) {
  21. int[] copy = nums.clone();
  22. int n = nums.length;
  23. ArrayList<Integer> ls = new ArrayList<>();
  24. int i = 0;
  25.  
  26. while(i < n){
  27. int totMEX = calMEX(copy);
  28. Set<Integer> seen = new HashSet<>();
  29. int currMEX = 0;
  30. for(int j = i; j < n; j++){
  31. seen.add(nums[j]);
  32. while(seen.contains(currMEX)){
  33. currMEX++;
  34. }
  35. if(currMEX == totMEX){
  36. ls.add(totMEX);
  37. copy = Arrays.copyOfRange(nums, j + 1, n);
  38. i = j + 1;
  39. break;
  40. }
  41. }
  42. }
  43. int[] ans = new int[ls.size()];
  44. for(int k = 0; k < ls.size(); k++){
  45. ans[k] = ls.get(k);
  46. }
  47. return ans;
  48. }
  49.  
  50. public static void main (String[] args) throws java.lang.Exception
  51. {
  52. // your code goes here
  53. int[] nums = {0,1,0};
  54. int[] temp = maximumMEX(nums);
  55. for(int i : temp){
  56. System.out.println(i);
  57. }
  58. }
  59. }
Success #stdin #stdout 0.07s 54576KB
stdin
Standard input is empty
stdout
2
1