diff --git a/file2.txt b/file2.txt deleted file mode 100644 index 5852f44..0000000 --- a/file2.txt +++ /dev/null @@ -1 +0,0 @@ -Initial commit diff --git a/file3.txt b/file3.txt deleted file mode 100644 index 5852f44..0000000 --- a/file3.txt +++ /dev/null @@ -1 +0,0 @@ -Initial commit diff --git a/file4.txt b/file4.txt deleted file mode 100644 index 5852f44..0000000 --- a/file4.txt +++ /dev/null @@ -1 +0,0 @@ -Initial commit diff --git a/src/com/array/rotations/SubArraySample.java b/src/com/array/rotations/SubArraySample.java new file mode 100644 index 0000000..a0d0024 --- /dev/null +++ b/src/com/array/rotations/SubArraySample.java @@ -0,0 +1,89 @@ +package com.array.rotations; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class SubArraySample { + + // Create in integer array (1,...10) + // input n = 2 + // output should be [1,2] [3,4] + + + public static void main(String arg[]) { + List ll = Arrays.asList(1,5,2,3, 5,4,6,1,7, 8,2,9, 3,4); + int n =5; + System.out.println( + IntStream + .range(0, (ll.size() + n - 1) / n) + .mapToObj(i -> ll.subList(i * n, Math.min((i + 1) * n, ll.size()))) + .collect(Collectors.toList())); + + List> result = null;//getSubArray(ll, n); + //result.forEach(System.out::println); + } + + private static List> getSubArray(List ll, int n) { + + List> result = new ArrayList<>(); + List subArray = new ArrayList<>(); + for (Integer num : ll) { + subArray.add(num); + if(subArray.size() == n) { + result.add(subArray); + subArray = new ArrayList<>(); + } + } + + if(!subArray.isEmpty()) { + result.add(subArray); + } + + + return IntStream + .range(0, (ll.size() + n - 1) / n) + .mapToObj(i -> ll.subList(i * n, Math.min((i + 1) * n, ll.size()))) + .collect(Collectors.toList()); + + //return result; + } + + public static void main1(String arg[]) { + List ll = Arrays.asList(1,5,2,3, 5,4,6,1,7, 8,2,9, 3,4); + int n =2; + List> sequences = subArrayBasedOnIndex(ll, n); + sequences.forEach(System.out::println); + + List finalResult = sequences + .stream() + .flatMap(list->list.parallelStream()) + .sorted(Comparator.reverseOrder()) + .collect(Collectors.toList()); + + System.out.println(finalResult); + } + + private static List> subArrayBasedOnIndex(List ll, int n) { + List> sequences = new ArrayList<>(); + List subSeq = new ArrayList<>(); + + for (int index=0; index < ll.size() ; index++) { + subSeq.add(ll.get(index)); + if(subSeq.size() == n) { + sequences.add(subSeq); + subSeq = new ArrayList<>(); + } + } + + if(!subSeq.isEmpty()) { + sequences.add(subSeq); + } + + return sequences; + } + +} diff --git a/file1.txt b/test.txt similarity index 100% rename from file1.txt rename to test.txt