diff --git a/src/com/.DS_Store b/src/com/.DS_Store index e3f6b96..e716a71 100644 Binary files a/src/com/.DS_Store and b/src/com/.DS_Store differ diff --git a/src/com/array/rotations/ArrayAverage.java b/src/com/array/rotations/ArrayAverage.java new file mode 100644 index 0000000..fd362c5 --- /dev/null +++ b/src/com/array/rotations/ArrayAverage.java @@ -0,0 +1,29 @@ +package com.array.rotations; + +public class ArrayAverage { + + public static void main(String[] args) { + int n = 10; + int[] arr = new int[n]; + + // Input array elements + System.out.println("Enter the elements of the array: "); + for (int i = 0; i < n; i++) { + arr[i] = 1+i; + } + + // Calculate sum of elements + int sum = 0; + for (int i = 0; i < n; i++) { + sum += arr[i]; + } + + // Calculate average + double average = (double) sum / n; + + // Output the average + System.out.println("The average is: " + average); + + } + +} diff --git a/src/com/array/rotations/PeekSample.java b/src/com/array/rotations/PeekSample.java new file mode 100644 index 0000000..d017eeb --- /dev/null +++ b/src/com/array/rotations/PeekSample.java @@ -0,0 +1,29 @@ +package com.array.rotations; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class PeekSample { + + public static void main(String arg[]) { + List names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Edward"); + + List upperNames = names + .stream() + .peek(n-> System.out.println("Names : "+n)) + //.map(n-> n) + .collect(Collectors.toList()); + + upperNames.stream().forEach(n -> System.out.println(n)); + + String listToString = upperNames + .stream() + //.map(n->n) + .collect(Collectors.joining(", ", "[", "]")); + + + System.out.println(listToString); + } + +} 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 diff --git a/file2.txt b/test1.txt similarity index 100% rename from file2.txt rename to test1.txt diff --git a/file3.txt b/test2.txt similarity index 100% rename from file3.txt rename to test2.txt