• + 0 comments

    One more optimization can be done: instead of trasversing whole array we could traverse array between indices - lower of left indices and higher of right indices

    A psudo code is as follows:

    
    for (i=0;i<row;i++){
        if (queries[i][0]-1 < low){
            low = queries[i][0]-1;
        }
        if (queries[i][1]-1 > high){
            high = queries[i][1]-1;
        }
    }
    
    for (i=low;i<=high;i++){
                temp += a[i];
                if(max < temp){
                    max = temp;
                }
            }
    return max;