Concept explainers
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]"...
Trending nowThis is a popular solution!
Chapter 5 Solutions
Data Structures and Algorithms in Java
- Give a recursive definition of the following set of ordered pairs of positive integers: S = {(a, b)| (a is element of positive integer), (b is an integer multiplied by 3)}arrow_forwardWrite a recursive function using python for the problem: A palindrome is a sequence of characters which is the same when the sequence is reversed. Given an array A indexed from p to q containing characters, determine if the sequence of characters in A[p..q] forms a palindrome. Return True is a palindrome is formed and return False if it does not.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
- write java codes to do the following: Write a recursive function to check if an integer array is negative symmetric, for example, an array of 10, 20, 30, 90, -30, -20, -10 is considered as negative symmetric, while an array of 10, 20, 30, 90, 30, 20, 10 is not considered as negative symmetric. public static boolean checkArraySym(int [ ] A, int first, int last) that receives an array A, first index, last index and checks if the array is negative symmetricarrow_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_forwardFor function addOdd(n) write the missing recursive call. This function should return the sum of all postive odd numbers less than or equal to n. Examples: addOdd(1) -> 1addOdd(2) -> 1addOdd(3) -> 4addOdd(7) -> 16 public int addOdd(int n) { if (n <= 0) { return 0; } if (n % 2 != 0) { // Odd value return <<Missing a Recursive call>> } else { // Even value return addOdd(n - 1); }}arrow_forward
- Using recursion, write a Python function def before(k,A) which takes an integer k and an array A of integers as inputs and returns a new array consisting of all the integers in A which come before the last occurrence of k in A, in the same order they are in A. For example, if A is [1,2,3,6,7,2,3,4] then before(3,A) will return [1,2,3,6,7,2]. If k does not occur in A, the function should return None.arrow_forwardThe Tower of Hanoi is a puzzle where n disks of different sizes arestacked in ascending order on one rod and there are two other rods with nodisks on them. The objective is to move all disks from the first rod to thethird, such that:- only one disk is moved at a time- a larger disk can never be placed on top of a smaller oneWrite a recursive function that outputs the sequence of steps needed tosolve the puzzle with n disks.Write a test program in C++ that allows the user to input number of disks andthen uses your function to output the steps needed to solve the puzzle.Hint: If you could move up n−1 of the disks from the first post to thethird post using the second post as a spare, the last disk could be moved fromthe first post to the second post. Then by using the same technique you canmove the n−1 disks from the third post to the second post, using the firstdisk as a spare. There! You have the puzzle solved. You only have to decidewhat the nonrecursive case is, what the recursive…arrow_forwardYour 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. Sierpinski.java 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 with the specified side length. public static double height(double length) // Draws a filled equilateral triangle with the specified side length // whose bottom vertex is (x, y). public static void filledTriangle(double x, double y, double length) // Draws a Sierpinski triangle of order n, such that the largest filled //…arrow_forward
- 4. Consider the function IndexEqual(A,i.j) that returns true if there exists an index x (i sx sj) such that A[x] = x; otherwise, retums false. You may assume A is a sorted integer array in which every element is unique. a. Write an efficient recursive algorithm for IndexEqual(A,i.j). b. What is the situation resulting in the best-case running time of your function, and give an expression for that running time? c. What is the situation resulting in the worst-case running time of your function, and give an expression for that running time in terms of n, where n=j-i+1?arrow_forwardBy, using java. give an reverse recursivefunction to print elements in arrayarrow_forwardfunction recursion(B[0..n − 1], i) if n == 0 then return False if n == 1 then return (B[0] == i)x ← recursion(B[0........n/2 − 1], i) y ← recursion(B[n/2..........4 × n/2 − 1], i) z ← recursion(B[4 × n/2..........n − 1], i) return (x OR y OR z) input array is of length n = 2^p, p is a positive integer Write the recursive formula for above algorithm as of worst case inputs.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