I am working with a linked list that I created that contains a set of numbers as data. I need to find a way to check all possible two sets of sections of this list for something, and for this I need to break the list into all kinds of combinations with two sets. The order is not important, and there will be duplicates.
For instance, for a list of numbers {1 4 3 1}, the possible splits are {1} and {4, 3, 1} {4} and {1, 3, 1} {3} and {1, 4, 1} {1} and {1, 4, 3} {1, 4} and {3, 1} {1, 3} and {4, 1} {1, 1} and {4, 3}
A 4-digit list is not complicated, but things get complicated as the list grows larger and I can’t see the template. Can someone help me find an algorithm for this?
Edit:
Sorry, I did not see the question. This is what I have tried so far. My loop structure is incorrect. When I find out what I'm doing after checking for a regular array, I will expand the algorithm to fit my linked list.
public class TwoSubsets { public static void main(String[] args) { int[] list = {1, 3, 5, 7, 8}; int places = 1; int[] subsetA = new int[10]; int[] subsetB = new int[10]; for (int i = 0; i < list.length; i++) { subsetA[i] = list[i]; for (int current = 0; current < (5 - i ); current++) { subsetB[current] = list[places]; places++; } System.out.print("subsetA = "); for (int j = 0; j < subsetA.length; j++) { System.out.print(subsetA[j] + " "); } System.out.println(); System.out.print("subsetB = "); for (int k = 0; k < subsetB.length; k++) { System.out.print(subsetB[k] + " "); } } } }
source share