0

This is a solution for Max sum K-partitions problem, when I use array declarations it shows Runtime error, but works fine with vector declarations. Can't figure out why? How they are different?

Max Sum K-partitions Problem Statement: Siraj has an array of N integers and an integer K. He wants to create a subsequence of this array with some conditions applied. He builds ceil(N/K) blocks of this array with size of each block as [i∗K+1,min((i+1)∗k,N)] for 0≤i≤N/K. For two consecutive integers in this subsequence, he wants the integers to be of two different blocks. (It is not a compulsion to have one integer from each block). Also, lets say the indexes of any two integers of this subsequence be i and j, then he wants |i−j| != K. Siraj takes the sum of integers in the subsequence. He wonders what can be the maximum sum obtained? Help siraj in this problem.

#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;

ll maxsum(ll arr[], ll n, ll k){

    ll dp[n]={0};   // vector<ll> dp(n,0);
    ll max1=INT_MIN;
    ll max2=INT_MIN;
    ll maxind=-1;
    ll i = 0;
    for(i = 0 ; i < k ;i++){
        dp[i] = arr[i];
        if(arr[i] > max1){
            max2 = max1;
            max1 = arr[i];
            maxind = i;
            continue;
        }
        max2 = max(max2 , arr[i]);
    }


    while(count--){
        pos = i;
        for( ; i < min(n, pos+k) ; i++){
            dp[i] = arr[i];
            if(arr[i] >= 0 ){
                if(abs(i-maxind) != k){
                    dp[i] += max1;
                }else
                dp[i] += max2;
            }else
            dp[i] = max1;
        }

        for(i  = pos ; i < min(n,pos+ k) ; i++){
            if(dp[i] > max1){
                max2 = max1;
                max1 = dp[i];
                maxind = i;
                continue;
            }
            max2 = max(max2, dp[i]);
        }
    }    
    return max1;
}
int main(){
    ll n,k;
    cin>>n>>k;
    ll a[n];   //vector<ll> a(n);
    for(int i=0;i<n;i++)
        cin>>a[i];
    cout<<maxsum(a,n,k)<<"\n";
return 0;
}

  • How big is `n`? At a guess you're running out of stack space because of your non-standard Variable Length Arrays. `vector` is your friend here; why don't you want to use it? – 1201ProgramAlarm May 25 '20 at 18:09
  • @1201ProgramAlarm n is in range 1<= n<= 2e6. – Tarandeep97 May 25 '20 at 18:13
  • The main difference is that `ll a[n];` is not legal C++, since `n` is not a compile time constant, as is required by C++. – john May 25 '20 at 18:18
  • https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h/31816096 – Werner Henze May 25 '20 at 19:11
  • @john Thanks. Is this difference specific to a C++ version, because it works on my local compiler but shows RE on online compiler. – Tarandeep97 May 27 '20 at 07:11
  • @Tarandeep97 `ll a[n]` where `n` is not a compile time constant has never been legal C++. However it is legal C (in later versions of C). This is where the confusion arises because some C++ compilers accept this kind of array, but others don't. But the legality of this array is probably not the issue here. RE indicates that your program is at least running. When you're trying to create a very large object its common that it doesn't work in some environments. I haven't looked at your problem in any detail, but I guess the point of the task is to solve it without needing to create a huge array. – john May 27 '20 at 08:12
  • @john Thanks for such a clear explanation! – Tarandeep97 May 31 '20 at 11:22

0 Answers0