|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package coding.bat.solutions; |
| 5 | + |
| 6 | +/** |
| 7 | + * @author Aman Shekhar |
| 8 | + * |
| 9 | + */ |
| 10 | +public class ArraysThree { |
| 11 | + |
| 12 | + /** |
| 13 | + * @param args |
| 14 | + */ |
| 15 | + public static void main(String[] args) { |
| 16 | + // TODO Auto-generated method stub |
| 17 | + |
| 18 | + } |
| 19 | + |
| 20 | + // -------------------------------------------------------------------------------------------- |
| 21 | + |
| 22 | + // Consider the leftmost and righmost appearances of some value in an array. |
| 23 | + // We'll say that the "span" is the number of elements between the two |
| 24 | + // inclusive. A single value has a span of 1. Returns the largest span found in |
| 25 | + // the given array. (Efficiency is not a priority.) |
| 26 | + // |
| 27 | + // |
| 28 | + // maxSpan([1, 2, 1, 1, 3]) → 4 |
| 29 | + // maxSpan([1, 4, 2, 1, 4, 1, 4]) → 6 |
| 30 | + // maxSpan([1, 4, 2, 1, 4, 4, 4]) → 6 |
| 31 | + |
| 32 | + public int maxSpan(int[] nums) { |
| 33 | + int maxSpan = 0; |
| 34 | + int span; |
| 35 | + int j; |
| 36 | + for (int i = 0; i < nums.length; i++) { |
| 37 | + for (j = nums.length - 1; nums[i] != nums[j]; j--) |
| 38 | + ; |
| 39 | + span = 1 + j - i; |
| 40 | + if (span > maxSpan) |
| 41 | + maxSpan = span; |
| 42 | + } |
| 43 | + return maxSpan; |
| 44 | + } |
| 45 | + |
| 46 | + // -------------------------------------------------------------------------------------------- |
| 47 | + |
| 48 | + |
| 49 | + // Return an array that contains exactly the same numbers as the given array, |
| 50 | + // but rearranged so that every 3 is immediately followed by a 4. Do not move |
| 51 | + // the 3's, but every other number may move. The array contains the same number |
| 52 | + // of 3's and 4's, every 3 has a number after it that is not a 3, and a 3 |
| 53 | + // appears in the array before any 4. |
| 54 | + // |
| 55 | + // |
| 56 | + // fix34([1, 3, 1, 4]) → [1, 3, 4, 1] |
| 57 | + // fix34([1, 3, 1, 4, 4, 3, 1]) → [1, 3, 4, 1, 1, 3, 4] |
| 58 | + // fix34([3, 2, 2, 4]) → [3, 4, 2, 2] |
| 59 | + |
| 60 | + public int[] fix34(int[] nums) { |
| 61 | + int j = 1; |
| 62 | + for (int i = 0; i < nums.length - 1; i++) { |
| 63 | + if (nums[i] == 3 && nums[i + 1] != 4) { |
| 64 | + for (; nums[j] != 4; j++) |
| 65 | + ; |
| 66 | + nums[j] = nums[i + 1]; |
| 67 | + nums[i + 1] = 4; |
| 68 | + } |
| 69 | + } |
| 70 | + return nums; |
| 71 | + } |
| 72 | + |
| 73 | + // -------------------------------------------------------------------------------------------- |
| 74 | + |
| 75 | + |
| 76 | + // (This is a slightly harder version of the fix34 problem.) Return an array |
| 77 | + // that contains exactly the same numbers as the given array, but rearranged so |
| 78 | + // that every 4 is immediately followed by a 5. Do not move the 4's, but every |
| 79 | + // other number may move. The array contains the same number of 4's and 5's, and |
| 80 | + // every 4 has a number after it that is not a 4. In this version, 5's may |
| 81 | + // appear anywhere in the original array. |
| 82 | + // |
| 83 | + // |
| 84 | + // fix45([5, 4, 9, 4, 9, 5]) → [9, 4, 5, 4, 5, 9] |
| 85 | + // fix45([1, 4, 1, 5]) → [1, 4, 5, 1] |
| 86 | + // fix45([1, 4, 1, 5, 5, 4, 1]) → [1, 4, 5, 1, 1, 4, 5] |
| 87 | + |
| 88 | + public int[] fix45(int[] nums) { |
| 89 | + int j = 0; |
| 90 | + for (int i = 0; i < nums.length - 1; i++) { |
| 91 | + if (nums[i] == 4 && nums[i + 1] != 5) { |
| 92 | + for (; !(nums[j] == 5 && (j == 0 || j > 0 && nums[j - 1] != 4)); j++) |
| 93 | + ; |
| 94 | + nums[j] = nums[i + 1]; |
| 95 | + nums[i + 1] = 5; |
| 96 | + } |
| 97 | + } |
| 98 | + return nums; |
| 99 | + } |
| 100 | + |
| 101 | + // -------------------------------------------------------------------------------------------- |
| 102 | + |
| 103 | + |
| 104 | + // Given a non-empty array, return true if there is a place to split the array |
| 105 | + // so that the sum of the numbers on one side is equal to the sum of the numbers |
| 106 | + // on the other side. |
| 107 | + public boolean canBalance(int[] nums) { |
| 108 | + int left = 0; |
| 109 | + int right; |
| 110 | + for (int i = 0; i < nums.length - 1; i++) |
| 111 | + left += nums[i]; |
| 112 | + right = nums[nums.length - 1]; |
| 113 | + for (int i = nums.length - 2; i > 0; i--) { |
| 114 | + if (left == right) |
| 115 | + return true; |
| 116 | + left -= nums[i]; |
| 117 | + right += nums[i]; |
| 118 | + } |
| 119 | + return (left == right); |
| 120 | + } |
| 121 | + |
| 122 | + // -------------------------------------------------------------------------------------------- |
| 123 | + |
| 124 | + |
| 125 | + // Given two arrays of ints sorted in increasing order, outer and inner, return |
| 126 | + // true if all of the numbers in inner appear in outer. The best solution makes |
| 127 | + // only a single "linear" pass of both arrays, taking advantage of the fact that |
| 128 | + // both arrays are already in sorted order. |
| 129 | + public boolean linearIn(int[] outer, int[] inner) { |
| 130 | + boolean notFound; |
| 131 | + for (int inI = 0, outI = 0; inI < inner.length; inI++) { |
| 132 | + notFound = true; |
| 133 | + for (; outI < outer.length && notFound; outI++) { |
| 134 | + if (inner[inI] == outer[outI]) |
| 135 | + notFound = false; |
| 136 | + } |
| 137 | + if (notFound) |
| 138 | + return false; |
| 139 | + } |
| 140 | + return true; |
| 141 | + } |
| 142 | + |
| 143 | + // -------------------------------------------------------------------------------------------- |
| 144 | + |
| 145 | + |
| 146 | + // Given n>=0, create an array length n*n with the following pattern, shown here |
| 147 | + // for n=3 : {0, 0, 1, 0, 2, 1, 3, 2, 1} (spaces added to show the 3 groups). |
| 148 | + public int[] squareUp(int n) { |
| 149 | + int[] arr = new int[n * n]; |
| 150 | + int p; |
| 151 | + for (int i = 1; i <= n; i++) { |
| 152 | + p = n * i - 1; |
| 153 | + for (int j = 1; j <= i; j++, p--) |
| 154 | + arr[p] = j; |
| 155 | + } |
| 156 | + return arr; |
| 157 | + } |
| 158 | + |
| 159 | + // -------------------------------------------------------------------------------------------- |
| 160 | + |
| 161 | + |
| 162 | + // Given n>=0, create an array with the pattern {1, 1, 2, 1, 2, 3, ... 1, 2, 3 |
| 163 | + // .. n} (spaces added to show the grouping). Note that the length of the array |
| 164 | + // will be 1 + 2 + 3 ... + n, which is known to sum to exactly n*(n + 1)/2. |
| 165 | + public int[] seriesUp(int n) { |
| 166 | + int[] arr = new int[n * (n + 1) / 2]; |
| 167 | + int p = 0; |
| 168 | + for (int i = 1; i <= n; i++) { |
| 169 | + for (int j = 1; j <= i; j++, p++) |
| 170 | + arr[p] = j; |
| 171 | + } |
| 172 | + return arr; |
| 173 | + } |
| 174 | + |
| 175 | + |
| 176 | + // -------------------------------------------------------------------------------------------- |
| 177 | + |
| 178 | + // We'll say that a "mirror" section in an array is a group of contiguous |
| 179 | + // elements such that somewhere in the array, the same group appears in reverse |
| 180 | + // order. For example, the largest mirror section in {1, 2, 3, 8, 9, 3, 2, 1} is |
| 181 | + // length 3 (the {1, 2, 3} part). Return the size of the largest mirror section |
| 182 | + // found in the given array. |
| 183 | + public int maxMirror(int[] nums) { |
| 184 | + int span; |
| 185 | + int maxSpan = 0; |
| 186 | + int left; |
| 187 | + int right; |
| 188 | + for (int i = 0; i < nums.length; i++) { |
| 189 | + left = i; |
| 190 | + right = lastIndexOf(nums, nums[i], nums.length - 1); |
| 191 | + while (right != -1) { |
| 192 | + span = 0; |
| 193 | + left = i; |
| 194 | + do { |
| 195 | + left++; |
| 196 | + right--; |
| 197 | + span++; |
| 198 | + } while (left < nums.length && right >= 0 && nums[left] == nums[right]); |
| 199 | + if (span > maxSpan) |
| 200 | + maxSpan = span; |
| 201 | + right = lastIndexOf(nums, nums[i], right); |
| 202 | + } |
| 203 | + } |
| 204 | + return maxSpan; |
| 205 | + } |
| 206 | + |
| 207 | + // helper function for maxMirror |
| 208 | + public int lastIndexOf(int[] nums, int value, int index) { |
| 209 | + for (; index >= 0; index--) { |
| 210 | + if (nums[index] == value) |
| 211 | + return index; |
| 212 | + } |
| 213 | + return -1; |
| 214 | + } |
| 215 | + |
| 216 | + // -------------------------------------------------------------------------------------------- |
| 217 | + |
| 218 | + |
| 219 | + // Say that a "clump" in an array is a series of 2 or more adjacent elements of |
| 220 | + // the same value. Return the number of clumps in the given array. |
| 221 | + |
| 222 | + |
| 223 | + public int countClumps(int[] nums) { |
| 224 | + int clumps = 0; |
| 225 | + boolean isClump = false; |
| 226 | + for (int i = 0; i < nums.length - 1; i++) { |
| 227 | + if (isClump) { |
| 228 | + if (nums[i] != nums[i + 1]) |
| 229 | + isClump = false; |
| 230 | + } else if (nums[i] == nums[i + 1]) { |
| 231 | + isClump = true; |
| 232 | + clumps++; |
| 233 | + } |
| 234 | + } |
| 235 | + return clumps; |
| 236 | + } |
| 237 | + |
| 238 | +} |
0 commit comments