Write a program called Split in Java that reads a text file, in.txt, that contai
ID: 3733707 • Letter: W
Question
Write a program called Split in Java that reads a text file, in.txt, that contains a list of positive integers (duplicates are possible, zero is not considered a positive integer) separated by spaces and/or line breaks. After reading the integers, the program prints out Yes if the set of integers can be split into two subsets with equal sums of elements and with equal numbers of elements. Otherwise (if the list if integers cannot be divided into two subsets satisfying the condition above), the program prints out No. Assume that in.txt contains at least 2 integers. (a) Source code (b) A report (not to exceed two pages) as an ASCI text document, MS Word document, or a PDF file that contains a description of the ALGORITHM implemented by your program and an analysis of its complexity using Big O notation. It is important that you write a description of the algorithm, not a description of your program! In other words, do not explain your classes and methods. Explain: 1 The sequence of operations that you use to solve the problem, and 2) Why this sequence of operations correctly solves the problem. Pseudo-code is a standard way of explaining algorithms. Examples: If in.txt contains 7 7, the program must print out Yes. In this case, the split is (7) and (7). Both sets are of size of 1, and have the same sum of elements. If in.txt contains 5 3 2 4, the program must print out Yes. The split is (2, 5), (3, 4). Both sets have the same size, 2, and the same sum, 7 If in.txt contains 5 7 5 1 1 3, the program must print out Yes. The split is {1, 55) and {1, 3, 7·Both sets have three elements and the same sum, 11. If in.txt contains 658344, the program must print out Yes. The split is (4, 5, 6) and (3, 4, 8). Both sets have the same length, 3, and the same sum, 15. If in.txt contains 2 6 10 14 4 8 12 16. There are several splits satisfying the requirement: 12, 8, 10, 16) and (4, 6, 12, 14); (4, 6, 10, 16) and (2, 8, 12, 14); (4, 8, 10, 14) and (2, 6,12,16); (6, 8, 10, 12) and (2, 4, 14, 16). Your program does not need to find all of them. It must stop and print Yes after finding the first split satisfying the requirement.Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Split {
List<Integer> values = new ArrayList<>();
Split(List<Integer> values)
{
this.values = values;
}
private String isPerfectSplit()
{
int sum = 0,j=0;
if(values.size()%2 != 0)
{
return "No";
}
for(int value : values)
sum+=value;//get the total sum
for(int i=0;i<values.size();i++)
while(j<values.size())
{
if(i!=j)
{
int k=0,count=0,splitSum=0,l=0;
while(count < ((values.size())/2)-1)
{
l = j+k;
if(l >= values.size())
l-=values.size();
if(i != l)
{
splitSum+=values.get(l);
count++;
}
k++;
}
splitSum+=values.get(i);//calculating the sum of each subset
if(splitSum == sum/2)//verify whether sum of the subset is half of the total sum
return "Yes";
}
j++;
}
j=0;
return "No";
}
public static void main(String args[])
{
List<Integer> values =new ArrayList<Integer>();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(
"/in.txt"));
String line = reader.readLine();
while (line != null) {
values.add(Integer.parseInt(line));
// read next line
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
Split s = new Split(values);
System.out.println(s.isPerfectSplit());
}
}
***************************Description****************************************************
Split Program
Description:
To find out if a set of integers split into two subsets with equal size, then their respective sums are equal or not. The following steps are done:
1. Find the sum of all the values in the set.
2.Create multiple subsets with each subset's size as half as the total size of the set.
3.Calculate the sum of all the subsets.
4. If the sum of a partcular subset is exactly half of the total sum.
5.Then print Yes, otherwise print No.
Complexity:
The complexity is as follows:
T(n) = n(n-1)((n/2)-1)
T(n) = (n2-n)((n/2-1))
T(n) = n3/2-n2/2-2n
The Big O notation is O(n3)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.