Question

In: Computer Science

Question: Use backtracking algorithm design to write Java code to solve the subset problem: given a...

Question: Use backtracking algorithm design to write Java code to solve the subset problem: given a set of distinct integers, return all possible subsets. for example, input: new int[] {1,2,3}

output: [], [3], [2], [2,3], [1], [1,3], [1,2], [1,2,3]

Solutions

Expert Solution

/* Subsets.java */


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class Subsets {

public static List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> sets = new ArrayList<>(); // create result list
Arrays.sort(nums);
backtrack(sets, new ArrayList<>(), nums, 0, nums.length);
return sets;
}
/**
* Backtracking
* Time Complexity: O(n^2)
* Space Complexity: O(n^2)
*/
private static void backtrack(List<List<Integer>> sets, List<Integer> set, int[] nums, int start, int end) {
sets.add(new ArrayList<>(set)); // add subset each time inside sets list
for (int i = start; i < end; i++) {
set.add(nums[i]);
backtrack(sets, set, nums, i + 1, end);// find all subset
set.remove(set.size() - 1);// remove extra
}
}
  
public static void main(String[] args) {
int num[] = {1,2,3};
//System.out.println(subsets(num)); // can print directly [[],[1]..]
// or convert to array and print subsets
Object sets[] = subsets(num).toArray();
for (int i = 0; i < sets.length; i++) {
System.out.print(sets[i]);
if(i < sets.length-1){
System.out.print(",");
}
  
}
System.out.println("");
}
}

/* OUTPUT */

/* PLEASE UPVOTE */


Related Solutions

Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array...
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array of integers into ascending order. Consider the given array as input and produce a sorted array as output. Each time you take an integer from the input array, place it at the end of the output array. If the result is unsorted, backtrack.
Language for this question is Java write the code for the given assignment Given an n...
Language for this question is Java write the code for the given assignment Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of matrix in sorted order.Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the matrix. Then the next line contains the n x n elements...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a method to test if a LinkedList<Long> has Long values that form a Fibonacci sequence from the beginning to the end and return true if it is and false otherwise. A sequence of values is Fibonnaci if every third value is equal to sum of the previous two. Eg., 3,4,7,11,18,29 is a Fibonacci sequence whereas 1,2,3,4 is not, because 2+3 is not equal to 4....
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) Requirements Choose one problem with an algorithm and implement it. You should show and explain the result whatever you got. I recommend using N-Queen problem (at least N=8 or more) or any simple perfect games. For example, - N-Queen problem with hill climbing - N-Queen problem with simulated annealing - N-Queen problem with genetic algorithm - Tic-Tac-Toe with Minimax
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) N-Queen problem with genetic algorithm Please use the N-Queen problem (at least N=8 or more) or any simple perfect games. Please provide a screenshot of output and please heavily comment the code. Thanks!
Use java .Given a stack S and an element x, write a recursive algorithm that returns...
Use java .Given a stack S and an element x, write a recursive algorithm that returns True if x is in S, or False otherwise. Note that your algorithm should not change the content in S. What is the time complexity of your algorithm?
Please use C programming to write the code to solve the following problem. Also, please use...
Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...
given an array, write code to scan the array for a particular purpose . use java
given an array, write code to scan the array for a particular purpose . use java
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find out the total number of inversions in the given sequence. For example, for the sequence of 2, 4, 1, 3, 5, there are three inversions (2,1), (4,1), and (4,3). Give a brute-force algorithm with running time of O(n^2). Using the technique of divide-and-conquer, design an algorithm with running time of O(nlog n).
problem 1 (Duplicate Elimination) code in JAVA please Use a one-dimensional array to solve the following...
problem 1 (Duplicate Elimination) code in JAVA please Use a one-dimensional array to solve the following problem: Write an application that inputs ten numbers, each between 10 and 100, both inclusive. Save each number that was read in an array that was initialized to a value of -1 for all elements. Assume a value of -1 indicates an array element is empty. You are then to process the array, and remove duplicate elements from the array containing the numbers you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT