Concept explainers
Explanation of Solution
Recursive method to determine the sum of two elements is equal to “n”:
Create the method FindPair() that accepts the input parameter as “a”, “low”, “high”, and “n” to determine the sum of two elements in an array “a” is equal to “n” by calling this method recursively.
Method definition:
//Define the FindPair() method
public static boolean FindPair(int a[], int low, int high, int n)
{
//Check whether the "low" is equal to "high"
if(low == high)
/*Return false, if there are no elements in array "a" that sum is equal to "n". */
return false;
/*Otherwise, check whether the sum of "a[low]" and "a[high]" is less than "n". */
else if((a[low]+a[high]) < n)
/*Call FindPair() method recursively by passing the parameters as "a", "low+1", "high" and "n" to sum the two elements is equal to "n". */
return FindPair(a, low+1, high, n);
/*Otherwise, check whether the sum of "a[low]" and "a[high]" is greater than "n". */
else if((a[low]+a[high]) > n)
/*Call FindPair() method recursively by passing the parameters as "a", "low", "high-1" and "n" to sum the two elements is equal to "n". */
return FindPair(a, low, high-1, n);
else
/*Return true, if there are elements in array "a" that sum is equal to "n". */
return true;
}
Explanation:
In the above code,
- In the FindPair() method,
- It accepts the input parameter as “a”, “low”, “high”, and “n”.
- Check whether the “low” is equal to “high”. If yes,
- Return false, if there are no elements in array “a” that sum is equal to “n”.
- Otherwise, check whether the sum of “a[low]” and “a[high]” is less than “n”. If yes,
- Call FindPair() method recursively by passing the parameters as “a”, “low+1”, “high”, and “n” to sum the two elements is equal to “n”...
Want to see the full answer?
Check out a sample textbook solutionChapter 5 Solutions
Data Structures and Algorithms in Java
- Give a recursive algorithm for finding the sum of the first n positive integersarrow_forwardThe Polish mathematician Wacław Sierpiński described the pattern in 1915, but it has appeared in Italian art since the 13th century. Though the Sierpinski triangle looks complex, it can be generated with a short recursive function. Your main task is to write a recursive function sierpinski() that plots a Sierpinski triangle of order n to standard drawing. Think recursively: sierpinski() should draw one filled equilateral triangle (pointed downwards) and then call itself recursively three times (with an appropriate stopping condition). It should draw 1 filled triangle for n = 1; 4 filled triangles for n = 2; and 13 filled triangles for n = 3; and so forth. API specification. When writing your program, exercise modular design by organizing it into four functions, as specified in the following API: public class Sierpinski { // Height of an equilateral triangle whose sides are of the specified length. public static double height(double length) // Draws a filled equilateral…arrow_forwardLet n be a positive integer and let MaxCrossing(n) be a function that returns the maximum number of line crossings that you can create by drawing n straight lines. Write down a recursive formula for MaxCrossing(n) 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.arrow_forward
- Give a recursive algorithm for the sequential search and explain its running time.arrow_forwardWrite a recursive implementation of Euclid’s algorithm for finding the greatest common divisor (GCD) of two integers. Descriptions of this algorithm are available in algebra books and on the Web. Write a test program that calls your GCD procedure five times, using the following pairs of integers: (5,20), (24,18), (11,7), (432,226), (26,13). After each procedure call, display the GCD.arrow_forwardBy using java, Write a recursive function that finds the maximum element in an array ofintegers, based on comparing the first element in the array against themaximum in the rest of the array recursively.arrow_forward
- Design and implement a recursive program(in java) to determine and print the Nth line of Pascal's triangle, as shown below. Each interior value is the sum of the two values above it. Hint: Use an array to store the values on each line.arrow_forwardWrite a recursive implementation of Euclid's Algorith for finding the greatest common divisor(GCD) of two intergers. Descriptions of this algorithm are available in algebra books and on theweb. (Note: A nonrecursive version of the GCD problem was given in the programming exercisesfor Chapter 7.) Write a test program that calls your GCD procedure five times, using thefollowing pairs of integers: (5,20),(24,18),(11,7),(432,226),(26,13). After each procedure call,display the GCD.arrow_forwardLet n be a positive integer and let MaxCrossing(n) be a function that returns the maximum number of line crossings that you can create by drawing n straight lines. Write down a recursive formula for MaxCrossing(n) 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. Note: if n=1, there is 0 crossing, if n = 2, there is 1 crossing, if n = 3, there are 3 crossings (not only 2) if n = 4, there are 6 crossings (not only 3) if n = 5, there are 10 crossings, etc.arrow_forward
- Write and test a Java/Python recursive method for finding the minimum element in an array, A, of n elements. What the running time? Hint: an array of size 1 would be the stop condition. The implementation is similar to linearSum method in lecture 5 examples. You can use the Math.min method for finding the minimum of two numbers.arrow_forwardAssume that for each number I n is not 2. How could the algorithm be modified to handle the situation where n is odd? I have two approaches: one that directly adjusts the recursive method and the other that mixes the iterative and recursive approaches. Just one of the two tasks must be completed (as long as it works and does not increase the BigOh of the running time.)arrow_forwardWrite a recursive function for Euclid's algorithm to find the greatest common divisor (gcd) of two positive integers. gcd is the largest integer that divides evenly into both of them. For example, the gcd(102, 68) = 34. You may recall learning about the greatest common divisor when you learned to reduce fractions. For example, we can simplify 68/102 to 2/3 by dividing both numerator and denominator by 34, their gcd. Finding the gcd of huge numbers is an important problem that arises in many commercial applications. We can efficiently compute the gcd using the following property, which holds for positive integers p and q: If p > q, the gcd of p and q is the same as the gcd of q and p % q.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education