Java | Easy | Length of Longest Cycle
\nclass Solution {\n     static class Pair{\n         int val;\n         int idx;\n       \n         Pair(int val,int idx){\n           this.val = val;\n       
```\nclass Solution {\n     static class Pair{\n         int val;\n         int idx;\n       \n         Pair(int val,int idx){\n           this.val = val;\n           this.idx = idx;\n         }\n     }\n    public int arrayNesting(int[] arr) {\n        //This question is nothing but finding the length of longest cycle\n        int n = arr.length;\n        Pair p[] = new Pair[n];\n      \n        for(int i = 0; i < n ;i++){\n           p[i] = new Pair(arr[i],i);\n        }\n      \n        Arrays.sort(p,(a,b)->{\n          return a.val - b.val;\n        });\n      \n        boolean visited[] = new boolean[n];\n        int maxLenCycle = 1;\n      \n        for(int i = 0 ; i <  n ; i++){\n           if(visited[i] == true || p[i].idx == i){\n             continue;\n           }\n          int len = 0;\n          int j = i;\n          \n          while(visited[j] == false){\n             visited[j] = true;\n             len++;\n             j = p[j].idx;\n          }\n          maxLenCycle = Math.max(len,maxLenCycle);\n        }\n      return maxLenCycle;\n    }\n}\n```
[]