Length of list pattern

Viewed 67

I have a list where I am trying to find the sum of combination of the lists entries, except the entries where both values to add are equal to each other (ie 2+2 would not be added) and add them to another list. As an example:

[1,2,3] would yield the list of sums [3,4,5] because 1+2=5,1+3=4, and 2+3=5

However, my issues arises with not knowing how many sums will be produced. I am working in java and am limited to native arrays, therefore the size of the array has to be set before I can add the sum values to it.

I know I would not be able to find the exact size of the sum list due to the possibility that a sum would not get added if the two elements are the same, but I am trying to ballpark it so I don't have massive arrays.

The closest 'formula' I have gotten is setting the following, but it is never precisely what the max value would be for any list

(list length of original numbers * list length of original numbers) / 2

I am trying to keep time complexity in mind, so keeping a running count of how many sums there are, setting an array to that size, and looping through the original list again would not be efficient.

Any suggestions?

1 Answers

Can you add same sums to array, I mean, your array is {1,2,3,4,5}. Would you print the both result of 1+5 and 2+4 =6. If your answer is yes. You can get the length of array and multiply it with 1 less and divide them to 2. For instance; our array → {1,2,3,4,5} the lenght is 5 the length of our result array will be 5*4/2=10.

Or you can use lists in java if you cant define a length for array. Keep in mind.

Related