Write a recursive method that will compute cumulative sums in an array. To find the cumulative sums, add to each value in the array live sum of the values that precede it in the array. For example, it the values in the array are [2, 3, 1, 5, 6, 2, 7], the result will be [2, (2) + 3, (2 + 3) + 1, (2 + 3 + 1) + 5, (2 + 3 + 1 + 5) + 6, (2 + 3 + 1 + 5 + 6) + 2, (2 + 3 + 1 + 5 +6 + 2) + 7] or [2, 5, 6, 11, 17, 19, 26]. (Hint: The parenthesized sums in the previous example are the results of a recursive call.)
Want to see the full answer?
Check out a sample textbook solutionChapter 11 Solutions
Java: An Introduction to Problem Solving and Programming (7th Edition)
Additional Engineering Textbook Solutions
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Database Concepts (7th Edition)
Software Engineering (10th Edition)
C++ How to Program (10th Edition)
Java How To Program (Early Objects)
Starting Out with C++: Early Objects (9th Edition)
- write a recursive method that will sum up all elements in an array and returm the result. name the method as arraySumarrow_forwardwrite a code in java (using recursion)arrow_forwardGiven an array of length N and an integer x, you need to find all the indexes where x is present in the input array. Save all the indexes in an array (in increasing order). Do this recursively. Indexing in the array starts from 0. Input Format : Line 1: An Integer Ni.e. size of array Line 2: N integers which are elements of the array, separated by spaces Line 3: Integer x Output Format : indexes where x is present in the array (separated by space) Constraints: 1 <= N <= 10^3 Sample Input: 5 981088 8 Sample Output : 134 Solution:///// public class Solution { public static int[] allIndexes (int input[], int x, int startIndex) { if(startIndex==input.length) { int output[] = new int[0]; return output; } int smallOutput[] = allIndexes (input,x, startIndex+1); if(input[startIndex]==x){ int output[] = new int[smallOutput.length +1]; } } output[0] = startIndex; for(int i=0;iarrow_forwardThe following recursive method is causing an exception because the base case is missing. This method is suppose to display the content of an array backward. For example If the array num contains 1 2 3 4 5 6 then the call print(num, 5) should display: 6 5 4 3 2 1 public static void print(int[] a, int index){ System.out.println(a[index]); print(a, index -1);}Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 5 fill in the blank so that the exception does not happen, in order of the given numbers public static void print(int[] a, int index){if( __1___ ___2__ ___3__) <----- base case __4____ System.out.println(a[index]); print(a, index -1); <---- recursive case}arrow_forwardJava Programming : At a fun fair, a street vendor is selling different colours of balloons. He sells N number of different colours of balloons (B[]). The task is to find the colour (odd) of the balloon which is present odd number of times in the bunch of balloons. Note: If there is more than one colour which is odd in number, then the first colour in the array which is present odd number of times is displayed. The colours of the balloons can all be either upper case or lower case in the array. If all the inputs are even in number, display the message 'All are even'. Example 1: 7 -> Value of N [r,g,b,b,g,y,y] -> B[] Elements B[0] to B[N-1], where each input element is separated by new line. Output: r -> [r,g,b,b,g,y.y] →> 'r' colour balloon is present odd number of times in the bunch. Explanation: From the input array above: r: 1 balloon g: 2 balloons b: 2 balloons y: 2 balloons Hence, r is only the balloon which is odd in number.arrow_forwardsolve q2 only pleasearrow_forwardWrite java code that recursively calculates the Sum of the array cells. Program requirements: Create an array named MyArr and initialize it as follow: 243 88 31 19 27 81 100 6 10 5 1- Write a method named ArrSum that recursively calculates the Sum of the numbers in the Array and returns the Sum of the numbers. Hint: Method with 1 parameter (the Array) that returns 1 value (the Array's Sum). 2- Write a method named Display that displays the Array contents and Sum of the numbers. Hint: Method with 2 parameters (the Array) and Sum with no returns (void) 3- Write a method named ArrSort using the bubble sort to sort Array. 4- Your program output format must be the same as the following: =>> Array Summation Array: 5 6 10 19 27 31 81 88 100 243 Sum: 610 =>> End of the Program Be sure to add Documentation / Commentsarrow_forwardYou will be given an array A of length n. Write a method to calculate the following value of X using recursion. X = Ao x 7° + A1 x 7' + Az × 7² +. +An-1 x 7"-1 • The recursion can have any number of parameters. You can't use loops or math.pow() or a separate recursive function to calculate 7". The return type should be integer. • The range for n is 1arrow_forwardIn javaarrow_forwardWrite a recursive method called printReverse. The method shall take an array of char and will print the content in reverse order in the same line. The method shall use public and private method.arrow_forwardWhat is the solution of this problem using java language?arrow_forwardJava Programming : A party has been organised on cruise. The party is organised for a limited time (T). The number of guests entering (E[i]) and leaving (L[i]) the party at every hour is represented as elements of the array. The task is to find the maximum number of guests present on the cruise at any given instance within T hours. Example 1: Input : 5 > Value of T [7,0,5,1,3] -> E[], Element of E[0] to E[N-1], where input each element is separated by new line [1,2,1,3,4] -> L[], Element of L[0] to L[N-1], while input each element is separate by new line. Output : 8 -> Maximum number of guests on cruise at an instance. Explanation: 1st hour: Entry 7 Exit: 1 No. of guests on ship : 6 2nd hour : Entry Exit : 2 No. of guests on ship: 6-2=4 Hour 3: Entry: 5 Exit: 1 No. of guests on ship : 4+5-1-8 Hour 4: Entry 1 Exit 3 No. of guests on ship: 8+1-3=6 Hour 5: Entry 3 Exit: 4 No. of guests on ship: 6+3-4-5 Hence, the maximum number of guests within 5 hours is 8.arrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_iosRecommended textbooks for you
- 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
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:PEARSONC 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