I would like to return or save for later use in the ParallelSums function what I currently have printing out to the console in the findAllSubsetsRec function.
If I pass the ParallelSums function an int array of [16, 22, 35, 8, 20, 1, 21, 11] I currently get the console output from the findAllSubsetsRec of:
22 20 16 8 1
22 21 16 8
35 20 11 1
35 21 11
I would like each line that is seen above to be returned to my ParallelSums function, probably in multiple lists or in a way that I can later put them in multiple lists/arrays, so that I can do some further processing on them.
Below is the code I have so far:
using System;
using System.Linq;
using System.Collections.Generic;
class MainClass {
static void findAllSubsetsRec(int []arr, int n, List<int> v, int target) {
List<int> subList = new List<int>();
// if the remaining target is 0, add elements to subList
if(target == 0) {
for (int i=0; i<v.Count; i++) {
Console.Write(v[i]+ " ");
subList.Add(v[i]);
}
Console.WriteLine();
return;
}
// if there are no elements remaining return
if (n == 0) {
return;
}
// two cases to consider for every element
// 1. we won't include the last element
// 2. we will include the last element in current subset
findAllSubsetsRec(arr, n-1, v, target);
List<int> v1 = new List<int>(v);
v1.Add(arr[n-1]);
findAllSubsetsRec(arr, n-1, v1, target-arr[n-1]);
}
public static int ParallelSums(int[] arr) {
// variable initialization
// manually set number of sub arrays we are looking to use
// so we can write code to work for anything and then restrict
// it to work for this particular case
int numOfSubArr = 2;
// determine if we have enough elements in arr to allow for
// an even number of elements for each sub array per requirements
int evenNumElementsPerArr = arr.Length%numOfSubArr;
// find our target number for our sub arrays to equal
int arrSum = arr.Sum();
int target = arr.Sum()/numOfSubArr;
int possibleTarget = arrSum%numOfSubArr;
Console.WriteLine("possibleTarget = " + possibleTarget);
Console.WriteLine("Target Number = {0}", target);
Console.WriteLine("Possible target for equal length sub arrays? " + ((possibleTarget==0)?"yes":"no"));
Console.WriteLine("Even number of elements per array? " + ((evenNumElementsPerArr==0)?"yes":"no"));
// validation checks
if (evenNumElementsPerArr != 0 || possibleTarget != 0) {
Console.WriteLine("Array of odd numbered length or can't be equally divided into sub arrays");
return -1;
} else {
Console.WriteLine("We have an even number of elements and sub array lengths");
// initialize arrays and variables for sums
int n = arr.Length;
List<int> v = new List<int>();
// sort our input array
Array.Sort(arr);
findAllSubsetsRec(arr, n, v, target);
}
// will eventually return my desired results, but for now will just return 1
return 1;
}
static void Main() {
// keep this function call here
Console.WriteLine(ParallelSums(Console.ReadLine()));
}
}
The end goal is to take all of these lists and determine which 2 contain all the numbers from the input array, without using any twice, while both lists have an equal length/count of elements.
In my example below the list of [22,21,16,8] and [35,20,11,1] would ultimately be the two correct lists to solve my problem.
Any help would be very much appreciated and I'm open to changing my methodology if something else makes more sense than what I'm doing now.