|
| 1 | +package easy; |
| 2 | + |
| 3 | +/** |
| 4 | + * Have the function LargestFour(arr) take the array of integers stored in arr, |
| 5 | + * and find the four largest elements and return their sum. |
| 6 | + * -- |
| 7 | + * For example: if arr is [4, 5, -2, 3, 1, 2, 6, 6] |
| 8 | + * then the four largest elements in this array are 6, 6, 4, and 5 |
| 9 | + * and the total sum of these numbers is 21, so your program should return 21. |
| 10 | + * -- |
| 11 | + * If there are less than four numbers in the array your program |
| 12 | + * should return the sum of all the numbers in the array. |
| 13 | + */ |
| 14 | +public class LargestFour { |
| 15 | + |
| 16 | + /** |
| 17 | + * QuickSelect implementation based on Sedgewick R., Wayne K., Algorithms 4th edn. |
| 18 | + * |
| 19 | + * @param a input array |
| 20 | + * @param k k-smallest element in the list |
| 21 | + */ |
| 22 | + private static void select(int[] a, int k) { |
| 23 | + int lo = 0; |
| 24 | + int hi = a.length - 1; |
| 25 | + while (hi > lo) { |
| 26 | + int i = partition(a, lo, hi); |
| 27 | + if (i > k) { |
| 28 | + hi = i - 1; |
| 29 | + } else if (i >= k) { |
| 30 | + return; |
| 31 | + } else { |
| 32 | + lo = i + 1; |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private static int partition(int[] a, int lo, int hi) { |
| 38 | + int i = lo; |
| 39 | + int j = hi + 1; |
| 40 | + int v = a[lo]; |
| 41 | + while (true) { |
| 42 | + // find item on lo to swap |
| 43 | + while (less(a[++i], v)) { |
| 44 | + if (i == hi) { |
| 45 | + break; |
| 46 | + } |
| 47 | + } |
| 48 | + // find item on hi to swap |
| 49 | + while (less(v, a[--j])) { |
| 50 | + if (j == lo) { |
| 51 | + break; // redundant since a[lo] acts as sentinel |
| 52 | + } |
| 53 | + } |
| 54 | + // check if pointers cross |
| 55 | + if (i >= j) { |
| 56 | + break; |
| 57 | + } |
| 58 | + exchange(a, i, j); |
| 59 | + } |
| 60 | + // put partitioning item v at a[j] |
| 61 | + exchange(a, lo, j); |
| 62 | + // now, a[lo .. j-1] <= a[j] <= a[j+1 .. hi] |
| 63 | + return j; |
| 64 | + } |
| 65 | + |
| 66 | + private static boolean less(Comparable<Integer> v, Comparable<Integer> w) { |
| 67 | + return v.compareTo((Integer) w) < 0; |
| 68 | + } |
| 69 | + |
| 70 | + // exchange a[i] and a[j] |
| 71 | + private static void exchange(int[] a, int i, int j) { |
| 72 | + int swap = a[i]; |
| 73 | + a[i] = a[j]; |
| 74 | + a[j] = swap; |
| 75 | + } |
| 76 | + |
| 77 | + private static int largestFour(int[] arr) { |
| 78 | + int sum = 0; |
| 79 | + if (arr.length <= 4) { |
| 80 | + for (int i : arr) { |
| 81 | + sum += i; |
| 82 | + } |
| 83 | + return sum; |
| 84 | + } |
| 85 | + int k = arr.length - 4; |
| 86 | + select(arr, k); |
| 87 | + for (int i = k; i < arr.length; i++) { |
| 88 | + sum += arr[i]; |
| 89 | + } |
| 90 | + return sum; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Entry point. |
| 95 | + * |
| 96 | + * @param args command line arguments |
| 97 | + */ |
| 98 | + public static void main(String[] args) { |
| 99 | + int[] arr1 = new int[]{5, 6, 10, 12, 1, 1, 1, 5}; |
| 100 | + int result1 = largestFour(arr1); |
| 101 | + System.out.println(result1); |
| 102 | + |
| 103 | + int[] arr2 = new int[]{0, 0, 2, 3, 7, 1}; |
| 104 | + int result2 = largestFour(arr2); |
| 105 | + System.out.println(result2); |
| 106 | + |
| 107 | + int[] arr3 = new int[]{5, 6, 10, 12, 1, 1, 1, 5}; |
| 108 | + int result3 = largestFour(arr3); |
| 109 | + System.out.println(result3); |
| 110 | + } |
| 111 | +} |
0 commit comments