Facing difficulty in passing test case

  algorithm, c++, data-structures
 int maximumSumSubarray(int K, vector<int> &Arr , int N){
        int sum=0;
        if(N<K){
            return -1;
        }
        for(int i=1;i<N;i++){
            Arr[i]=Arr[i-1]+Arr[i];
        }
        int j=K-1;
        int range=0;
        int i=-1;
        while(Arr[j]){
            if(Arr[j]-range>sum){
                sum=Arr[j]-range;
            }
            i++;
            range=Arr[i];
            j++;
            
        }
        return sum ;
    }

question is to find maximum sum of subarray of size K.
Maaximum of the test cases are passing but facing difficulty in this one.

334 271
250 156 156 194 269 57 161 34 224 287 172 59 78 41 46 82 162 291 224 251 201 255 76 65 143 225 260 220 90 245 70 39 152 277 284 120 34 144 205 257 182 76 67 12 168 113 93 30 103 69 32 3 75 108 119 217 84 78 188 173 74 258 263 226 234 298 97 19 142 54 27 75 181 94 86 49 206 231 130 60 299 161 63 125 20 181 42 103 11 281 27 84 290 41 9 224 39 158 294 232 211 21 6 91 114 92 139 71 22 268 130 72 181 244 196 200 125 289 55 187 270 133 270 11 174 31 234 264 188 280 195 150 300 252 240 165 43 131 287 116 150 116 187 82 112 135 34 288 123 88 174 144 220 143 155 145 225 140 160 164 119 54 13 170 6 5 34 100 135 20 216 36 188 154 170 51 288 203 38 163 290 211 6 261 105 212 158 30 104 17 193 222 123 206 144 180 262 229 279 148 1 246 183 188 100 52 290 87 254 27 249 295 237 7 8 93 218 165 122 21 181 67 295 55 24 138 234 285 118 213 132 118 210 66 57 9 170 46 96 123 72 96 170 60 102 177 153 72 41 26 144 273 92 190 28 167 79 13 151 197 225 34 14 187 100 71 195 269 116 42 143 240 138 64 299 29……………..

Its Correct output is:
38757

And Your Code’s output is:
44264

Source: Windows Questions C++

LEAVE A COMMENT