Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program called Biggest in Java that reads a text file, in.txt , that contains a list of positive and negative integers (duplicate are

Write a program called Biggest in Java that reads a text file, in.txt, that contains a list of positive and negative integers (duplicate are possible) separated by spaces and/or line breaks. Zero is not included. After reading the integers, the program saves them in a singly linked list in the same order in which they appear in the input file. Then, without changing the linked list, the program prints out the size of the largest zero-sum subset of integers. If the list does not contain a subset of integers whose sum is zero, the program prints out zero. Assume that in.txt is in the same folder as your java files and that it contains at least one integer.

Zero-sum subset is a subset of integers whose sum is zero. A zero-sum subset is the largest among all zero-sum subsets if it has the maximum number of elements.

Examples:

If in.txt contains 2 -1 3, the program must print out 0. The list {2, -1, 3} does not include a subset of elements whose sum is 0.

If in.txt contains 10 8 -10 2, the program must print out 3. The zero-sum subsets are {10, -10} and {8, -10, 2}. The largest one is {8, -10, 2} and its size is 3.

If in.txt contains 9 8 18 -5 18 -2 -1, the program must print out 4. There is only one zero-sum subset {8, -5, -2, -1}. The size of the subset is 4.

If in.txt contains 10 -1 9 1 -9, the program must print out 4. There are four zero-sum subsets: {-1, 1}, {9, -9}, {10, -1, -9} and {-1, 9, 1, -9}. The largest zero-sum subset is {-1, 9, 1, -9} and its size is 4.

If in.txt contains -5 3 4 9 4 -21, the program must print out 0. The list does not include any zero-sum subsets.

The Program has to:

  1. Implement a linked list.

You are supposed to implement the linked list from scratch without using language libraries. For example, you are not allowed to use any built-in dynamic data structures, such as ArrayList or LinkedList. There is no need to provide a full-fledged implementation of linked list. Keep your linked list implementation minimal and implement as much as you need to solve the problem. Each node of the linked list must include only two elements: an integer and a reference to the next element from the list.

  1. Read the integers from the file in.txt and store them into the linked list in the same order in which they appear in the input file. No calculations are allowed at this step, the program must only read the input numbers and store them into the linked list.
  2. Work on the linked list in order to find out the size of the largest zero-sum subset. No additional data structures are allowed. All computations must be done in place on the original data structure. In other words, your program can use only one data structure (and only one instance of it), the linked list, some auxiliary scalar variables and reference variables (used to point to different nodes of the list). No additional data structures are allowed, such as a second linked list, an array, string, etc. The only exception is I/O where you can use strings to read in.txt. You can use standard I/O libraries to perform I/O. For example, you can use StringTokenizer or Scanner. This is the only place you can use strings and libraries. No data structures are allowed (with the exception of string used to read in.txt).
  3. Print the size of the largest zero-sum subset.
  4. Your program should not modify the linked list, i.e., it should remain in its original form as it was read from the input file in.txt.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions