Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781119278023
Author: Michael T. Goodrich; Roberto Tamassia; Michael H. Goldwasser
Publisher: Wiley Global Education US
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
D. S. Malik, Data Structures Using C++, 2nd Edition, 2010
Methods (Ch6) - Review 1. (The MyRoot method) Below is a manual implementation of the Math.sqrt() method in Java. There are two methods, method #1 which calculates the square root for positive integers, and method #2, which calculates the square root of positive doubles (also works for integers). public class SquareRoot { public static void main(String[] args) { } // implement a loop of your choice here // Method that calculates the square root of integer variables public static double myRoot(int number) { double root; root=number/2; double root old; do { root old root; root (root_old+number/root_old)/2; } while (Math.abs(root_old-root)>1.8E-6); return root; } // Method that calculates the square root of double variables public static double myRoot(double number) { double root; root number/2; double root_old; do { root old root; root (root_old+number/root_old)/2; while (Math.abs (root_old-root)>1.0E-6); return root; } } Program-it-Yourself: In the main method, create a program that…
I would like to know the main features about the following 3 key concepts:1. Backup Domain Controller (BDC)2. Access Control List (ACL)3. Dynamic Memory
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
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT