Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package codility;
- public class Solution {
- public int solution(int[] A){
- boolean[] visited = new boolean[A.length];
- int max = 0;
- int head;
- int tail;
- int counter;
- if(A.length > 0){
- for(int i=0; i<A.length; i++){
- counter = 1;
- head = i;
- tail = A[i];
- if(head != tail){
- if(!visited[head] || head == 0){
- visited[head] = true;
- visited[tail] = true;
- while(head != tail){
- tail = A[tail];
- visited[tail] = true;
- counter++;
- }
- }
- }
- if(counter > max)
- max = counter;
- }
- }
- return max;
- }
- public static void main(String[] args) {
- Solution solution = new Solution();
- int test[] = new int[] {5,4,0,3,1,6,2};
- System.out.println(solution.solution(test));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement