fork(1) 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 ArrayNesting
  9. {
  10. public int findSetS(int[] nums) {
  11. boolean[] visited = new boolean[nums.length];
  12. int res = 0;
  13. for (int i = 0; i < nums.length; i++) {
  14. if (!visited[i]) {
  15. int start = nums[i], count = 0;
  16. do {
  17. start = nums[start];
  18. count++;
  19. visited[start] = true;
  20. }
  21. while (start != nums[i]);
  22. res = Math.max(res, count);
  23. }
  24. }
  25. return res;
  26. }
  27.  
  28. public static void main(String[] args) {
  29.  
  30. ArrayNesting arrayNesting = new ArrayNesting();
  31.  
  32. int[] arr = new int[]{5, 4, 0, 3, 1, 6, 2};
  33. System.out.println(arrayNesting.findSetS(arr));
  34. }
  35. }
Success #stdin #stdout 0.05s 2184192KB
stdin
Standard input is empty
stdout
4