Maximize sum of subsets from two arrays having no consecutive values | Function to calculate maximum subset sum ; Initialize array to store dp states ; Base Cases ; Pre initializing for dp [ 0 ] & dp [ 1 ] ; Calculating dp [ index ] based on above formula ; Prmaximum subset sum ; Given arrays ; Length of the array
def maximumSubsetSum ( arr1 , arr2 , length ) : NEW_LINE INDENT dp = [ 0 ] * ( length + 1 ) NEW_LINE if ( length == 1 ) : NEW_LINE INDENT print ( max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) NEW_LINE return NEW_LINE DEDENT if ( length == 2 ) : NEW_LINE INDENT print ( max ( max ( arr1 [ 1 ] , arr2 [ 1 ] ) , max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT dp [ 0 ] = max ( arr1 [ 0 ] , arr2 [ 0 ] ) NEW_LINE dp [ 1 ] = max ( max ( arr1 [ 1 ] , arr2 [ 1 ] ) , max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) NEW_LINE index = 2 NEW_LINE while ( index < length ) : NEW_LINE INDENT dp [ index ] = max ( max ( arr1 [ index ] , arr2 [ index ] ) , max ( max ( arr1 [ index ] + dp [ index - 2 ] , arr2 [ index ] + dp [ index - 2 ] ) , dp [ index - 1 ] ) ) NEW_LINE index += 1 NEW_LINE DEDENT print ( dp [ length - 1 ] ) NEW_LINE DEDENT DEDENT arr1 = [ - 1 , - 2 , 4 , - 4 , 5 ] NEW_LINE arr2 = [ - 1 , - 2 , - 3 , 4 , 10 ] NEW_LINE length = 5 NEW_LINE maximumSubsetSum ( arr1 , arr2 , length ) NEW_LINE