java - Pinpointing defect in a method to find the maximum double slice sum of an array -


the task in question taken https://codility.com/programmers/lessons/9-maximum_slice_problem/max_double_slice_sum/.

i paraphrase task description here:

let slice of array (possibly empty) contiguous part of array. is, a[i],..,a[j-1] (where 0 <= <= j < length(a)) slice of a.

let sum of slice sum of elements contained within slice.

the task find maximal combined sum of pair of slices separated precisely 1 element in first , last element of array not present in either slice nor used separator. is, max{a[i+1]+..+a[j-1]+a[j+1]+..+a[k-1] | 0 <= < j < k < length(a)}.

my question not how solve task (i have found working solution online) rather why following solution fails work in cases. every test case have tried on succeeds, codility's large_random test produced wrong answer (my code produced: 2004640 correct answer: 2403731). unfortunately, not input caused error.

what causes below code return incorrect answer in exceptional cases?

public class maxdoubleslicesum {     public int solution(int[] a) {         final int n = a.length;         int minimum = integer.max_value;         int currentslice = 0;         int maxslice = 0;         integer dropvalue = null;         int uptodrop = 0;         (int = 1; < n - 1; i++){             if (a[i] < 0){                 if (dropvalue == null){                     dropvalue = a[i];                     uptodrop = currentslice;                         }                 else{                     /* find maximum of following:                      * 1. include a[i] , keep dropping dropvalue.                      * 2. make a[i] new dropped element , include dropvalue.                      * 3. make a[i] new dropped element , not include uptodrop , dropvalue.                      */                     int includingai = currentslice + a[i]; //1                     int includingolddropvalue = currentslice + dropvalue; //2                     int discardingolddropvalue = currentslice - uptodrop; //3                     if (includingai > includingolddropvalue && includingai > discardingolddropvalue){                         currentslice = includingai;                     }                     else if (includingolddropvalue > discardingolddropvalue){                         dropvalue = a[i];                         currentslice = includingolddropvalue;                         uptodrop = currentslice;                     }                     else{                         dropvalue = a[i];                         currentslice = discardingolddropvalue;                         uptodrop = currentslice;                     }                 }             }             else{                 currentslice += a[i];             }             maxslice = math.max(maxslice, currentslice);             minimum = math.min(minimum, a[i]);         }         if (dropvalue == null){             maxslice -= minimum;         }         return maxslice;     } } 


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -