Untitled
unknown
plain_text
a year ago
1.9 kB
12
Indexable
Never
public class MyClass { public static int[] joinArrays(int[] arr1, int[] arr2) { int[] res = new int[arr1.length+arr2.length]; for(int i=0; i < arr1.length+arr2.length;i++){ if (i < arr1.length) { res[i] = arr1[i]; } else { res[i] = arr2[i - arr1.length]; } } return res; } private static void printArray(String pretext, int[] arr) { System.out.println("\n" + pretext); for (int element: arr) { System.out.print(element + " "); } } public static int[] reduceArray(int[] arr, int start, int end, int k, String space){ System.out.println("\n" + space + "Start = " + start + ", End = " + end); if (end <= start) { int[] res = { arr[start] }; return res; } if ((end - start) == 1) { int prod = arr[start] * arr[end]; if (prod < k) { int[] res = { prod }; return res; } else { int[] res = { arr[start], arr[end] }; return res; } } else { int len = (end - start) / 2; int mid = start + len; int[] arr1 = reduceArray(arr, start, mid, k, space + " "); int[] arr2 = reduceArray(arr, mid+ 1, end, k, space + " "); int[] newArr = joinArrays(arr1, arr2); if (newArr.length == ((end-start) + 1)) { return newArr; } else { return reduceArray(newArr, 0, newArr.length-1, k, space + " "); } } } public static void main(String args[]) { int[] arr = {1,2,3,4,5,6,7,8}; int[] res = reduceArray(arr, 0, 7, 20, ""); printArray("Result: ", res); } }