0

The Basic idea is to use the Maximum subarray solution to generate all subarray of an array. But in order to implement that idea, I have been hit with a roadblock where I need to find all combinations of two vectors(left side of the mid and right side of the mid).

For example, if there are 5 elements in each of these vectors then if I choose 1 element from vector1 and then I have to choose one to five elements from vector2.

Similarly, if I choose 2 elements from vector1 then I have to choose one to five elements from vector2 etc.

The below is my try to implement the subarray for left and right sides.I need your help in implementing combinations of two vectors in c++.

for (int i = m; i >= l; i--) {
    sum = sum + arr[i];
    A[cnt].push_back(arr[i]);
    if (sum > left_sum)
        left_sum = sum;
}
int zsize=A[cnt].size();
int k1=cnt;
cnt++;
for (int i=0;i<zsize-1;i++)
{
   A[cnt]=A[k1];
   A[cnt].erase(A[cnt].begin()+i+1,A[cnt].begin()+zsize);
   cnt++;
}
8
  • If the first vector has N elements and the second has M, there are pow(2, M+N) combinations and factorial(M+N) + factorial(M+N-1) + factorial(M+N-2).... permutations. Commented Jun 11, 2021 at 16:24
  • Generally we go to great lengths in analysis of the problem to avoid having to actually work on all combinations/permutations (brute force). Commented Jun 11, 2021 at 16:26
  • Are you looking for contiguous subsets, which is what "subarray" usually means? Commented Jun 11, 2021 at 16:27
  • yes i am looking for contigous subsets to be generated less than O(n2) time Commented Jun 11, 2021 at 16:35
  • Well then you certainly cannot generate all of them. Commented Jun 11, 2021 at 16:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.