EBK BUILDING JAVA PROGRAMS
EBK BUILDING JAVA PROGRAMS
4th Edition
ISBN: 9780134323718
Author: Stepp
Publisher: PEARSON CUSTOM PUB.(CONSIGNMENT)
bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 12, Problem 20E

Explanation of Solution

Method definition:

//All the sub-lists of the given list of Strings gets printed.

/*elements!= null and elements contains no duplicates are the pre-condition*/

//method definition

public static void subsets(List<String> elements)

{

    //define the list

    List<String> chosen = new ArrayList<String>();

    //call the method explore

    explore(elements, chosen);

}

/*Helper recursive function defined to explore all the sub list from the given list of elements, choice is made assuming the given list of string is already chosen*/

//method definition

private static void explore(List<String> elements, List<String> chosen)

{

    // base case; nothing left to choose

    //validate whether the elements are empty

    if (elements.isEmpty())

    {

        //display the chosen

        System.out.println(chosen);

    }

    else

    {

        //define the choice that is for first element

        String first = elements.remove(0);

        // two explorations: one with this first element, one without

        //add the first element

        chosen.add(first);

        //call the explore method

        explore(elements, chosen);

        //remove the element

        chosen.remove(chosen.size() - 1);

        //call the method

        explore(elements, chosen);

        // backtrack! 1st element is put back

        elements...

Blurred answer
Students have asked these similar questions
Write a recursive function that finds the minimum value in an ArrayList. Your function signature should be public static int findMinimum(ArrayList<Integer>) One way to think of finding a minimum recursively is to think “the minimum number is either the last element in the ArrayList, or the minimum value in the rest of the ArrayList”. For example, if you have the ArrayList [1, 3, 2, 567, 23, 45, 9], the minimum value in this ArrayList is either 9 or the minimum value in [1, 3, 2, 567, 23, 45] ================================================ import java.util.*; public class RecursiveMin{public static void main(String[] args){Scanner input = new Scanner(System.in);ArrayList<Integer> numbers = new ArrayList<Integer>();while (true){System.out.println("Please enter numbers. Enter -1 to quit: ");int number = input.nextInt();if (number == -1){break;}else {numbers.add(number);}} int minimum = findMinimum(numbers);System.out.println("Minimum: " + minimum);}public static int…
Write a method subsets that uses recursive backtracking to find every possible sub-list of a given list. A sub-list of a list L contains 0 or more of L's elements. Your method should accept a List of strings as its parameter and print every sub-list that could be created from elements of that list, one per line. For example, suppose a variable called list stores the following elements: [Janet, Robert, Morgan, Char]The call of subsets(list); would produce output such as the following: [Janet, Robert, Morgan, Char][Janet, Robert, Morgan][Janet, Robert, Char][Janet, Robert][Janet, Morgan, Char][Janet, Morgan][Janet, Char][Janet][Robert, Morgan, Char][Robert, Morgan][Robert, Char][Robert][Morgan, Char][Morgan][Char][]​[3:07 AM]The order in which you show the sub-lists does not matter, but the order of the elements of each sub-list DOES matter (Note: this requirement is a limitation of Practice-It testing code. The textbook version of the problem would accept an answer with the elements in…
JAVA: Write a recursive method named threeSum that accepts a list of integers and prints all combinations of three integers in the list that sum to 0. For example, if given the list [-1, 0, 1, 2, -1, -4], print the following lines of output: [-1, 0, 1] [-1, 2, -1] [0, 1, -1] You may print the lines of output in any order. The elements in each three-element sublist should appear in the same relative order that they appeared in the original list. Do not print duplicate lists; if the same exact sublist can be made in multiple ways, print it only once. If there are no combinations of three elements that sum to 0, print no output. The list passed to your method must be back to its original state at the end of the call. Either do not modify it, or if you modify it, fully undo your modifications before the method returns. Constraints: Do not declare any global variables. You can use any data structures you like, and your code can contain loops, but the overall algorithm must be recursive and…
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
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7); Author: CS Dojo;https://www.youtube.com/watch?v=D6xkbGLQesk;License: Standard YouTube License, CC-BY