Maximum prefix sum for a given range | Practice | GeeksforGeeks

Given an array of N integers and Q queries, each query having a range from index L to R. Find the maximum prefix-sum for the range L to R.
Note: Assume 0 based indexing.
Example 1:
Input: 
a[ ] = {-1, 2, 3, -5} 
Q = 2
L1 = 0, R1 = 3
L2 = 1, R2 = 3
Output:
4 5
Explanation:
The range (0, 3) in the 1st query is {-1, 2, 3, -5}, hence, 
the max prefix-sum will be -1 + 2 + 3 = 4. The range (1, 3) 
in the 2nd query is {2, 3, -5}, hence, the max prefix-sum 
will be 2 + 3 = 5.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function maxPrefixes() which takes the array A[], its size N, array L[] and R[] and their size Q as input parameters and returns a vector storing the required answer for every query.
Expected Time Complexity: O(N*Q)
Expected Auxiliary Space: O(1)
Constraints: 
1 ≤ N, A[i] ≤ 104
1 ≤ Q ≤ 104
0 ≤ L[i] ≤ R[i] < N
0
gaurav119087902 weeks ago
for 0 to 3 range why it isn't 5 ?
 
0
harshrajvanshi16 months ago
people using python do have feelings bruh.....please dont hurt us with ur faulty drivers every time :'( 
+1
shaanrattan127 months ago
vector<int> maxPrefixes(int a[], int L[], int R[], int n, int Q) {
        vector<int> res;
        //prefix array
        int pfx[n], ind = 0;
        pfx[0]=a[0];
        for(int i=1;i<n;i++) pfx[i] = pfx[i-1]+a[i]; 
        
        while(Q--){
            int l = L[ind], r = R[ind], maximum = pfx[l];
            ind++;
            for(int i=l+1;i<=r;i++) maximum = max(maximum, pfx[i]);
            if(l==0) res.push_back(maximum);
            else res.push_back(maximum-pfx[l-1]);
        }
        return res;
    }
0
prakash kumar singh9 months ago
prakash kumar singh
can anyone help me with this code? what's the error here?
vector<int> maxPrefixes(int a[], int L[], int R[], int n, int Q) {        int prefixSum[n];        prefixSum[0]=a[0];        vector<int> vec;        for(int i=1;i<n;i++){ prefixsum[i]="prefixSum[i-1]+a[i];" }="" for(int="" i="0;i&lt;Q;i++){" if(l[i]!="0){" vec.push_back(prefixsum[r[i]]-prefixsum[l[i]-1])="" }="" else{="" vec.push_back(prefixsum[r[i]]);="" }="" }="" return="" vec;="">
0
aditya anand1 year ago
aditya anand
Why python has more driver faults, C'mon python folkes.please fix this errorprint(Solution().*maxPrefixes(a, l, r, n, q))
0
sachin chandela1 year ago
sachin chandela
Simple Java Solution ;https://uploads.disquscdn.c...
0
Shubhankit Bansal2 years ago
Shubhankit Bansal
https://ide.geeksforgeeks.o...
This code work properly with execution time: 0.15
0
G Vishal2 years ago
G Vishal
java using segment tree https://ide.geeksforgeeks.o... https://uploads.disquscdn.c... https://uploads.disquscdn.c...
0
G Vishal2 years ago
G Vishal
@GeeksForGeeks what is wrong with you i wrote entire python code to get tle whyhttps://ide.geeksforgeeks.o... https://uploads.disquscdn.c...
0
Bicky2 years ago
Bicky
simple cpp soln 0.1s
https://ide.geeksforgeeks.o...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.