2

I'm looking for advice on how to find all possible sums of N elements. For example, I have an array

int []arr={91,58,89,87,25,65,21};

For some int n=3;

I need to find all possible sums of 3 elements in this array. In the beginning, it looked easy for me as I would find all sums of subarrays of size 3 but I am not sure of subsequences of size 3 . I found similar questions on Google and the solution was to use recursion. I'm not sure I really understand how to solve this one with my conditions, hope you could give me an advise and example so I could solve this one! I'm not sure my initial code is need since it works linearly.

Thank you in advance.

KnightKnight
  • 217
  • 2
  • 16
eldlit
  • 63
  • 7

3 Answers3

2

You can easily solve this problem in O(n^3) time complexity using three for-loops iterating over each element.

Take a look at the python code below:

array = [91,58,89,87,25,65,21]
length = len(array)
n = 3

arraySum = []

for i in range(length - n + 1):
    sn = array[i]
    for j in range(i+1,length - n + 2):
        sn = sn + array[j]
        for k in range(j+1,length):
            sn = sn + array[k]
            arraySum.append(sn)

print(arraySum)
Yash Shah
  • 1,634
  • 1
  • 5
  • 16
2

I have this solution for variable length of subsequences. Java code:

public static void main(String[] args) {
    int[] arr = {91, 58, 89, 87, 25, 65, 21};
    int n = 3;

    sum(arr, 0, n, 0);
}

public static void sum(int[] arr, int startIdx, int deep, int currSum) {
    if (deep == 1) {
        for (int i = startIdx; i < arr.length; i++)
            System.out.println(currSum + arr[i]);
    } else {
        for (int i = startIdx; i < arr.length; i++)
            sum(arr, i + 1, deep - 1, currSum + arr[i]);
    }
}

UPD: If you need to get result as list, you may use this variation:

public static void main(String[] args) {
    int[] arr = {91, 58, 89, 87, 25, 65, 21};
    int n = 3;

    List<Integer> sums = sum(arr, n, 0);
    System.out.println(sums);
}

public static List<Integer> sum(int[] arr, int deep, int currSum) {
    List<Integer> list = new ArrayList<>();
    if (deep == 1) {
        for (int value : arr) list.add(currSum + value);
    } else {
        for (int i = 0; i < arr.length; i++)
            list.addAll(sum(Arrays.copyOfRange(arr, i + 1, arr.length), deep - 1, currSum + arr[i]));
    }
    return list;
}
Egor
  • 1,334
  • 8
  • 22
  • Thank you very much for your answer and your time. I've tried to apply your answer for my code, but I cant find a way to make return prototype `int`. Maybe you could help me with this as well. – eldlit Sep 20 '20 at 15:49
  • @eldlit please tell me more about your problem. What language do you use? You may show your code, it will be the easiest way. – Egor Sep 20 '20 at 17:25
  • I'm using Java and I need to get these numbers as an array or ArrayList so i could use them in my main method. I can't make it on my own since recursion is quite new for me, and i don't really get how to use return statement inside of these if-else statements. – eldlit Sep 20 '20 at 19:23
  • 1
    @eldlit I added solution that returning list to my answer – Egor Sep 21 '20 at 11:24
1

Please find this helpful because it seems you are not aware of the terms Subsequences and Subarrays. Subarrays and Subsequences what are they?

Now that if You know what a subsequence is, your question demands you to find count subsequences of size N which have distinct sums. I will attempt a recursive version of C++ code here , if you don't know C++ syntax I will try Java :).

void function(int size,int n,int sum,set<int> Set,int arr[],int iterator)
{
if(iterator>=arr.size())
    return;
if(size==n)
{
    Set.insert(sum);
    return;
}
function(size+1,n,sum+arr[iterator],Set,arr,iterator+1);
function(size, n,sum+arr[iterator],Set,arr,iterator+1);
}

The size of you Set will be your desired Answer.

KnightKnight
  • 217
  • 2
  • 16
  • Thank you very much. I know some C++ syntax as I studied some C at the University last year and now we are studying C++ and Java. Thank you very much for the link and for your answer as it is very helpful as any above. – eldlit Sep 20 '20 at 15:55