Question: //JAVA //COMMENTS WOULD BE VERY HELPFUL A peak in a square 2D int array is a value that is not smaller than its (at most)

//JAVA

//JAVA //COMMENTS WOULD BE VERY HELPFUL A peak in a square 2D

int array is a value that is not smaller than its (at

most) four neighbors (north, east, south or west are the only potential

neighbors). You must write a program that recursively searches for a peak

//COMMENTS WOULD BE VERY HELPFUL

A peak in a square 2D int array is a value that is not smaller than its (at most) four neighbors (north, east, south or west are the only potential neighbors). You must write a program that recursively searches for a peak in a square 2D int array using a recursive algorithm that will be reminiscent of (but substantially different than) Binary search. Your pro ram should first read an nt trom te user. hroughout this discussion, we will call this value size and we will assume that size is always a positive int. Then, you should generate a 2D int array that is size by size, whose elements are randomly generated positive ints that are all less than 100. Then, you should print out the array (formatted with even spacing). Finally, print out the value and location (row, col) of a valid peak returned by the recursive algorithm. You must find a valid peak using the recursive method illustrated by the forthcoming example: Columns are indexed, starting from the left, from 0 to 6. Here is how the recursive algorithm finds a valid peak. Denote left, right and middle with green, red, and yellow, respectively In the first call to the recursive method, left is 0 and right is 7 (note that 7 is not a valid column index, so there are no numbers in the red column in the following array) 55 2014756 78 21 24 3228 42 71 50 91 8261 60 5621 61 51 3648 3339 65 91 58 1720 16 81 65 16 8 4335 30 30 55 46 73 3538 13 48 4 75 Find the largest element in the middle column: 55 2014 7 5678 21 24 31228 42 71 50 91 8261 60 5621 61 51 3648 33 39 65 91 58 1720 16 81 65 16 8 433530 3055 46 73 3538 13 48 4 75 The largest element is 60 and it is NOT a peak because it is less than the value to its left (61). So there must be a peak in the left half of the array, which the algorithm will eventually return (what if the value to the right of 61 was larger?). In the next recursive call, left is 0 and right is 3 55 2014 756 78 21 24 312 28 42 71 50 91 8261 60 5621 61 51 3648 33 39 65 91 58 1720 16 81 65 16 8 4335 30 30 55 46 73 3538 13 |4847 Find the largest element in the middle column, with respect to columns left (inclusive) and right (exclusive). The largest element is 82 and it is still NOT a peak because it is less than the value to its left. So, there must be a peak in the left half of the portion of the array currently being considered (between left (inclusive) and right (exclusive)), which the algorithm will eventually return. In the next recursive cal1, left is 0 and right is 1 55 2014 7 56 78 21 24 312 28 42 71 50 918261 60 56 21 61 51 3648 33 3965 91 58 1720 16 81 65 16 8 4335 30 30 55 46 73 3538 13 4847 e are in the base case, because we are only considering elements in one column (the green column). Therefore, we return the location of the largest element (91) in column left, which is [2, 0]. So the method should return an array with two elements, where the first element is the row, and the second element is the column of the peak found Generalize this algorithm and implement the recursive method: public static int[] findPeakRec (int[] [] data, int left, int right)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!