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

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 5, Problem 23C

Explanation of Solution

Recursive method to determine the sum of two elements is equal to “n”:

Create the method checkSum() that accepts the input parameter as “A”, “j”, and “k” to determine the sum of two elements in an array “A” is equal to “A[i]” by calling this method recursively.

Method definition:

//Define the checkSum() method

public static boolean checkSum(int A[], int j, int k)

{

  //Loop executes until the array length

  for (int i = 0; i < A.length; i++)

  {

  //Check whether "j" and "k" is less than "i"

  if((i > j) && (k < i))

  {

/*Check whether sum of "A[j]" and "A[k]" is equal to "A[i]". */

  if (A[i] == A[j] + A[k])

  {

/*Return true, if there are elements in array "A" that sum is equal to "A[i]". */

  return true;

  }

  }

  }

//Check whether "k+1" is equal to length of array

  if (k + 1 == A.length)

  {

/*Check whether "j+1" is less than to length of array. */

  if (j + 1 < A.length)

  {

/*Call checkSum() method recursively by passing the parameters as "A", "j+1", and "0" to sum the two elements is equal to "A[i]" and return it */

  return checkSum(A, j + 1, 0);

  }

/*Return true, if there are no elements in array "A" that sum is equal to "A[i]"...

Blurred answer
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.
Let A[1...n,1...n] be a 2D array of positive integers with n rows and n columns. Let  ArraySum(a,b,c,d) be the sum of all the elements of A[a...b, c...d] (i.e., the submatrix of A with rows a to b and columns c to d).  Write down a recursive formula for ArraySum(a,b,c,d) and analyze the time complexity of the corresponding recursive algorithm. You must write a formal recursive formula including the base case and general recursive step.
Write a recursive function to determine if an array of integers contains any even numbers: bool hasEvens(int nums[], int size)
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education