We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
static int maxSubsetSumRecursion(vector& arr, int l, int r)
{
// TODO check null assert( arr );
// tolerating l > r as an indication of an empty set,
// but r - l shouldn't be less than -1
int size = (r-l)+1;
assert( size >= -1 );
if( size < 0 ) {
size = 0;
}
// Return base cases up to size == 2
if( 0 == size ) return 0;
if( 1 == size ) return max( arr[l], 0 );
if( 2 == size ) return max( max(arr[l], arr[r]), 0 );
// given a midpoint cell (index k = floor(n/2)):
// Case 1: int at cell [k] is in solution
// (Note: all ints in solution must be positive integers)
// Case 2: int at cell [k] is not in solution
const unsigned int k = l + (size/2); // floor division
int sum1 = 0;
if( arr[k] > 0 ){
sum1 = arr[k] +
maxSubsetSumRecursion( arr, l, k-2 ) +
maxSubsetSumRecursion( arr, k+2, r );
}
int sum2 = maxSubsetSumRecursion( arr, l, k-1 ) +
maxSubsetSumRecursion( arr, k+1, r );
return max(sum1, sum2);
}
'''
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Max Array Sum
You are viewing a single comment's thread. Return to all comments →
'''
static int maxSubsetSumRecursion(vector& arr, int l, int r) { // TODO check null assert( arr );
}
'''