3

I am having an array A which is having n elements. I want to find out the multiplication of all the elements in all the sub arrays possible of array A. I am expecting the solution to be implemented with the help of DP. I want to store all the product values in an array B. I am beginner to programming. I have done a lot of google search but i was unable to find the exact solution to my query. Can anybody help me to provide me the logic of the question. Example:

A={1,2,3}

All possible sub arrays are

{{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}}

so all possible products are

{1,2,3,2,3,6,6} 

respectively.

Any help is appreciable. Thanks in advance.

mynawaz
  • 1,599
  • 1
  • 9
  • 16
vivek sehgal
  • 59
  • 2
  • 4
  • @YoungHobbit actually i dont know how to solve this question for large n but i have searched on google and found some links which i didn't understood – vivek sehgal Oct 10 '15 at 07:26
  • 1
    You should consider using the correct word : Sub sequence instead of sub-array. Sub-arrays are contiguous parts of an array. – Tanishq Vyas Apr 04 '20 at 14:20

4 Answers4

0

this might help:

At every instance you have to make two choices:

Either you select the element of the array in the current sub-array or you don't.The following recursion might help:

f(i,p)=f(i+1,p*arr[i])||f(i+1,p)

RKS10222
  • 35
  • 3
0

you probably know how to create all the sub-arrays of an array, so, this make the problem easy:

if you have n element in your array(a) and one of the elements is m, then for calculating your problem, you can use this:

MySubArray(a , n) = new array{ MySubArray(a - {m} , n - 1) , MySubArray(a - {m} , n - 1) * m};

that means you once calculate the problem for all the subarrays of a - {m} and another time, you add m to all that and then multiplay their product by m.

Zhr Saghaie
  • 1,031
  • 2
  • 16
  • 41
0

There are N = 2^M-1 subsets for M-size set (including empty one), so every subset corresponds to number in range 0..N-1. If kth bit is set in some number, then kth element presents in corresponding subset.

Dynamic programming approach allows to reuse calculated products, so complexity is linear relative to output size O(N) = O(2^M)
Delphi code:

var
  A, Prods: TArray<Integer>;
  iA, i, SeriesLen, N: Integer;
  s: String;
begin
  A := TArray<Integer>.Create(2, 3, 5, 7);
  N := 1 shl Length(A); //output array size = 2^InputLength

  SetLength(Prods, N);
  Prods[0] := 1;
  SeriesLen := 1;
  for iA := 0 to Length(A) - 1 do begin
    for i := 0 to SeriesLen - 1 do
      Prods[i + SeriesLen] := Prods[i] * A[iA];
    SeriesLen := SeriesLen * 2;
  end;

output Prods[1]..Prods[N-1]

Result: 2 3 6 5 10 15 30 7 14 21 42 35 70 105 210 
MBo
  • 77,366
  • 5
  • 53
  • 86
0

A hint for you to try on your own would be something like this. Considering the fact that we have N elements in an array. The elements may or may not be unique. Then we create a list/array of binary digits to map all possible sub-arrays (Although correct term as per your doubt should be sub sequence).

For example : 
here N = 4.
Array : 1 2 3 2

The possible sub sequences would be

Binary Encoding (1 to include, 0 to exclude) :
0 0 0 0 : {}     # No use for us in this case
0 0 0 1 : {2}
0 0 1 0 : {3}
0 0 1 1 : {3, 2}
0 1 0 0 : {2}
0 1 0 1 : {2,2}
0 1 1 0 : {2,3}
0 1 1 1 : {2,3,2}
1 0 0 0 : {1}
1 0 0 1 : {1,2}
1 0 1 0 : {1,3}
1 0 1 1 : {1,3,2}
1 1 0 0 : {1,2}
1 1 0 1 : {1,2,2}
1 1 1 0 : {1,2,3}
1 1 1 1 : {1,2,3,2}


Thus you can generate all the sub sequences for a given array. and find their product.
Tanishq Vyas
  • 1,422
  • 1
  • 12
  • 25