From 9b6b37b4c1144df60cd5edf5e6aabd9c1ac30979 Mon Sep 17 00:00:00 2001 From: deadmarshal Date: Sat, 30 Sep 2023 21:13:24 +0330 Subject: Added python,java,raku --- challenge-236/deadmarshal/java/Ch2.java | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 challenge-236/deadmarshal/java/Ch2.java (limited to 'challenge-236/deadmarshal/java/Ch2.java') diff --git a/challenge-236/deadmarshal/java/Ch2.java b/challenge-236/deadmarshal/java/Ch2.java new file mode 100644 index 0000000000..a37ae7edc4 --- /dev/null +++ b/challenge-236/deadmarshal/java/Ch2.java @@ -0,0 +1,36 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class Ch2 { + public static void main(String[] args) { + ArrayList arr1 = new ArrayList<>( + List.of(4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10)); + ArrayList arr2 = new ArrayList<>( + List.of(0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19)); + ArrayList arr3 = new ArrayList<>( + List.of(9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17)); + System.out.println(array_loops(arr1)); + System.out.println(array_loops(arr2)); + System.out.println(array_loops(arr3)); + } + + private static int array_loops(List list) { + int count = 0, i = 0; + List indices = + new ArrayList(Collections.nCopies(list.size(), -1)); + for(; i < list.size(); ++i) + { + if(indices.get(i) == -1) + { + count++; + while(indices.get(i) == -1) + { + indices.set(i,1); + i = list.get(i); + } + } + } + return count; + } +} -- cgit