public static void main(String[] args) { int[] funky = {1, -5, 3, -5, 6, 6, 6, 7, -5}; int expectedFunkyRange = 5; // |6 - 1| = 5 System.out.println("Expected funkyRange of " + Arrays.toString(funky) + " to be " + expectedFunkyRange + " and got " + funkyRange(funky)); int[] funkyTwo = {6, 6, 4, 4, 4, 2, 2, 8, 4, 1, 1}; int expectedFunkyRangeTwo = 4; // |4 - 8| = 4 System.out.println("Expected funkyRange of " + Arrays.toString(funkyTwo) + " to be " + expectedFunkyRangeTwo + " and got " + funkyRange(funkyTwo)); Write a static method: public static int WeirdRange(int[] a) that that calculates the difference between the mode and "anti-mode" of an array of integers. This difference should be a positive number (you can use Math.abs to calculate an absolute value). If the array is empty or null, return -1. If the array has only one thing in it, return 0 (since the mode and anti-mode are the same). Definitions: The mode of an array is the element that occurs the most frequently. For this problem, if there is a tie for the mode, use the largest mode. In the following array: {1, -5, 3, -5, 6, 6, 6, 7, -5}, both 6 and -5 occur 3 times (which is the highest occurrence), but 6 is considered the mode as it is larger than -5. The anti-mode of an array is the element that occurs the least frequently. For this problem, if there is a tie for the anti-mode, use the smallest anti-mode. So in the following array: {1, -5, 3, -5, 6, 6, 6, 7, -5}, 1, 3, and 7 are the least frequently occurring numbers, but we consider 1 to be the anti-mode as it is the smaller than 3 and 7. Example1: funkyRange({1, -5, 3, -5, 6, 6, 6, 7, -5}) è 5, since the mode is 6 and the anti-mode is 1, and |6 – 1| = 5. Example2: funkyRange{6, 6, 4, 4, 4, 2, 2, 8, 4, 1, 1}. The result should be 4, since 4 is the mode and 8 is the anti-mode and |4 – 8| = 4. Example3: funkyRange. When empty and null arrays.Result should be -1 Example 4:weirdRange An array with one element in it.The result should be 0
public static void main(String[] args) {
int[] funky = {1, -5, 3, -5, 6, 6, 6, 7, -5};
int expectedFunkyRange = 5; // |6 - 1| = 5
System.out.println("Expected funkyRange of " + Arrays.toString(funky) +
" to be " + expectedFunkyRange + " and got " + funkyRange(funky));
int[] funkyTwo = {6, 6, 4, 4, 4, 2, 2, 8, 4, 1, 1};
int expectedFunkyRangeTwo = 4; // |4 - 8| = 4
System.out.println("Expected funkyRange of " + Arrays.toString(funkyTwo) +
" to be " + expectedFunkyRangeTwo + " and got " + funkyRange(funkyTwo));
Write a static method: public static int WeirdRange(int[] a) that that calculates the difference between the mode and "anti-mode" of an array of integers. This difference should be a positive number (you can use Math.abs to calculate an absolute value). If the array is empty or null, return -1. If the array has only one thing in it, return 0 (since the mode and anti-mode are the same).
Definitions:
The mode of an array is the element that occurs the most frequently. For this problem, if there is a tie for the mode, use the largest mode. In the following array: {1, -5, 3, -5, 6, 6, 6, 7, -5}, both 6 and -5 occur 3 times (which is the highest occurrence), but 6 is considered the mode as it is larger than -5.
The anti-mode of an array is the element that occurs the least frequently. For this problem, if there is a tie for the anti-mode, use the smallest anti-mode. So in the following array: {1, -5, 3, -5, 6, 6, 6, 7, -5}, 1, 3, and 7 are the least frequently occurring numbers, but we consider 1 to be the anti-mode as it is the smaller than 3 and 7.
Example1: funkyRange({1, -5, 3, -5, 6, 6, 6, 7, -5}) è 5, since the mode is 6 and the anti-mode is 1, and |6 – 1| = 5.
Example2: funkyRange{6, 6, 4, 4, 4, 2, 2, 8, 4, 1, 1}. The result should be 4, since 4 is the mode and 8 is the anti-mode and |4 – 8| = 4.
Example3: funkyRange. When empty and null arrays.Result should be -1
Example 4:weirdRange An array with one element in it.The result should be 0
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images