Write a java code to solve the following question using the backtracking algorithm.
Given a set of distinct integers, return all possible subsets.
input: new int[] {1,2,3}
output: [], [3], [2], [2,3], [1], [1,3], [1,2], [1,2,3]
What to submit: 1. Your source code in a text document (15pts), 2. A screen copy showing you can run the code and the result of running the code (5 pts). 3. What is the performance of your algorithm (5 pts), give the answer, and very brief discussion.
Please refer to the following code below:-
import java.util.ArrayList; import java.util.List; public class Backtracking { // main function to start the program public static void main(String[] args) { // Creating object for backtracking Backtracking b= new Backtracking(); // Declaring array int[] a= {1, 2, 3}; // creating list of lists List<List<Integer>> sub = b.sub(a); // printing all subsets System.out.println(sub); } // sub function public List<List<Integer>> sub(int[] a) { List<List<Integer>> list = new ArrayList<>(); // calling function to store elements of array subsets(list, new ArrayList<>(), a, 0); return list; } // this method will create subset private void subsets(List<List<Integer>> list , List<Integer> result, int [] a, int begin){ list.add(new ArrayList<>(result)); // iterating in array for(int i = begin; i < a.length; i++){ // add element result.add(a[i]); // Explore subsets(list, result, a, i + 1); // remove result.remove(result.size() - 1); } } }
Output:-
[[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]
========================================================
Please Refer to the following images:-
==========================
Time complexity:- O(2n)
===============================================
Please upvote
===============================================
Write a java code to solve the following question using the backtracking algorithm. Given a set...
1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...
use java and write in text a. Rewrite the following code segment using if statements. Assume that grade has been declared as of type char. char grade= 'B';; switch (grade) { case 'A': System.out.println("Excellent"); break case 'B': System.out.println("Good"); default: System.out.println("you can do better”); } - b. write Java code that inputs an integer and prints each of its digit followed by ** e.gif the integer is 1234 then it should print 1**2**3**4 e.g. if the integer is 85 then it...
Part 2 Write Java code for the following and create a word document with the answers. Test whether or not the following are true by writing the necessary statements in the driver class: 2, (A + B)T-AT + BT 4. (AB)- BA 6. A(BC) (AB)C I. (AT)% A 5. AB # BA // Java code to verify (A+B)C -AC + ВС Matrix anew Matrix(new int[](1,2),(2,0)); Matrix b new Matrix(new int1(11,2),(2,0); Matrix cnew Matrix(new int[1[1(í1,2),(2,0)); System.out.println(a.add(b).mult(c)); System.out.println(a.mult(c).add(b.mult(c))); For the following, write...
PLEASE SOLVE THESE QUESTIONS. USE JAVA FOR CODING. Q.5.8 Write short Java code to display the following screen (no need to do it practically using a compiler, just basic coding to display the screen): Sample screenshot Input х ? Enter (1) to display the vowel count. Enter (2) to display the non vowel count. Enter (0) to exit. OK Cancel Q.5.9 Write a short Java code to display the following screen (no need to do it practically using a compiler,...
Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should have short description of the implemented class and for files with main method the problem it is solving. Make sure your files have appropriate names. Programs should write output to the Console. b) BST: Implement Binary Search Tree ADT with insert(int key), delete(int key), Node find(int key), and in-order traverse() where it prints the value of the key. Your operations should use recursion. The...
I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 { public static...
Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code that uses selection sort to place the elements of an integer array into non-decreasing order is shown here: public void swapNumbers(int i, int j) { int temp = numbers[i]; /* put numbers[i] somewhere to keep it safe */ numbers[i] = numbers[j]; /* put numbers[j] at index i */ numbers[j] = temp; /* put numbers[i] at index j */ } public void selectionSort(int[] numbers) {...
Write the following Java code for a file called input.txt that has the below text in it. 5 6 1 1 0 0 1 1 0 1 0 0 1 1 0 1 0 0 2 1 0 3 1 1 1 1 1 2 3 4 5 1 2 0 1 hello 2 3 bye Create the Entry class. · Entry should have 3 data members o int x. o int y. o String name. Create a class called...
Hello, i am working on java program which implements Graph as an adjacency list structure. code is the implementation of RailNetwork. i need help to change the code so it could hold String instead of integers. so, for example, Station "Station name" is connected to "Station name" and also i need to add class edge which basically will be a direction for those give stations. so the output will be like "Station name" is connected to "Station name" with "this...