Data Structures and Algorithms in Java
6th Edition
ISBN: 9781118771334
Author: Michael T. Goodrich
Publisher: WILEY

#### Concept explainers

Chapter 5, Problem 1R

## Recursive method to find the maximum element in array:The recursive method to find the maximum element in array “A” of “n” elements is given below:The findMax() method returns the maximum elements in array.//Define the findMax() methodpublic static int findMax(int[] a, int n){  //Check whether the "n" is equal to "1"  if (n == 1)  //Return the maximum element  return a[0];/*Call the max() method and recursively call findMax() method*/return max(finMax(a, n - 1), a[n - 1]);}The max() method returns the maximum elements by comparing the array elements.//Define the max() methodprivate static int max(int n1, int n2){/*Check the "n1" is greater than "n2". If yes, return the maximum element. */  return n1 > n2 ? n1 : n2;}Explanation:In the above code, Check whether the “n” element is equal to “1”. If yes, It return the array of “a[0]”. That is, first element is a maximum element in given array “a”. Call the max() method to compare the array elements by calling the findMax() method recursively to find and return the maximum array element.Running time:The running time of above code is given below:According this code, The basic operation is comparison. That is, comparing the array elements recursively to find the maximum element in array. If there is no comparison. Then, the maximum array element is “a[0]” is “1”. Compare array value until “n-1” by calling the findMax() method. Then,  a(n) = n-1+1         = n Then, the array value is “a[n]” is “n”. Thus, it will recursively call the findMax() method to perform the “n” time of execution for “n” inputs. So, the usage space and time for this “n” array is O(n).Therefore, the running time and space of maximum element in array is O(n).

### Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
04:00
Students have asked these similar questions
Given an unsorted array, A, of integers and an integer k, write a recursivejava code for rearranging the elements in A so that all elements less than or equal to k come before any elements larger than k. What is the running time of your algorithm on an array of n values.
write a recursive function(in python) that computes the determinant of a (random)square matrix, represented as a list of lists of numeric values.   use main(3517) to produce the same number a the value above.
Give a recursive algorithm for finding the sum of the first n positive integers
Knowledge Booster
Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
• Copyright, Community Guidelines, DSA & other Legal Resources: Learneo Legal Center
• bartleby, a Learneo, Inc. business