3

I Implemented range max sum query using sparse Table ,I Know more efficient approach would be using segment trees.

What I have tried:

I am calculating the max sum in the range (i,2^j-1) for all possible value of i and j and storing them in a table

where i is the index and j denotes the power of 2 (2^j denotes the length of segment from i for which we are calculating max sum)

Now using the above table we can answer the queries

Input:

3

-1 2 3

1

1 2

expected output:

2

actual output:

"wrong answer(garbage value)"

we actually have to tell the max contiguous sum in a given query Link to the ques spoj gss1

Please help:

#include<iostream>
#include<vector>
#include<algorithm>
#include<climits>
using namespace std;
const int k = 16;
const int N = 1e5;
const int ZERO = 0; // ZERO + x = x + ZERO = x (for any x)

long long table[N][k + 1]; // k + 1 because we need to access table[r][k]
long long  Arr[N];

int main()
{
    int n, L, R, q;
    cin >> n; // array size
    for(int i = 0; i < n; i++)
        cin >> Arr[i];

    // build Sparse Table
    for(int i = 0; i < n; i++)
        table[i][0] = Arr[i];

    for(int j = 1; j <= k; j++) {
        for(int i = 0; i <= n - (1 << j); i++)
            //table[i][j] = table[i][j - 1] + table[i + (1 << (j - 1))][j - 1];
            table[i][j] = max(table[i][j-1],max(table[i+(1<<(j-1))][j-1],table[i+(1<<(j-1))][j-1]+table[i][j-1]));
    }

    cin >> q; // number of queries
    for(int i = 0; i < q; i++) {
        cin >> L >> R; // boundaries of next query, 0-indexed
        long long int answer = LLONG_MIN;
        for(int j = k; j >= 0; j--) {
            if(L + (1 << j) - 1 <= R) {
                answer = max(answer,answer + table[L][j]);
                L += 1 << j; // instead of having L', we increment L directly
            }
        }
        cout << answer << endl;
    }
    return 0;
}

link to the question Spoj Gss1

12341c0des
  • 31
  • 2
  • If you read all above, you should learn that for us to be able to help you, you need to actually *ask a question*. What is the problem you have with the code you show? What do you need our help with? Do the code give build errors? Crash at run-time? Give wrong results? – Some programmer dude Jan 20 '19 at 08:25
  • I am getting wrong answer sir – 12341c0des Jan 20 '19 at 08:26
  • Then please edit your question to show us the input you give the program, and the actual as well as expected results. I also recommend that you [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). And to help with debugging, split complex expressions into smaller and simpler expressions, to be able to see intermediate results. – Some programmer dude Jan 20 '19 at 08:28
  • Quote: "But my solution is not working" That is not a good problem description. You need to describe what goes wrong. For instance: What is your input? What is your expected output? What is your actual output? – Support Ukraine Jan 20 '19 at 08:28
  • Don't produce a pile of code and wonder why it doesn't work. Instead develop in increments. Start with a small working solution. Then expand it in small steps and make sure it works all the time. In the end you will have a full working solution. This is the essence of a program development method called Stepwise Refinement which was introduced by Wirth already in the 1970's. –  Jan 20 '19 at 08:36
  • For the loop `for(int i = 0; i <= n - (1 << j); i++)`, are you sure about `n - (1 << j)`? With your input (`n == 3`) then in the second iteration of the outer loop `1 << j` will be equal to `4`, which means `n - (1 << j)` will be negative, and the inner loop will not run again. – Some programmer dude Jan 20 '19 at 09:36
  • https://www.spoj.com/problems/GSS1/ question link – 12341c0des Jan 20 '19 at 09:46
  • for representing 3 we would require only two bits so for j=0 and j=1 loop will run – 12341c0des Jan 20 '19 at 09:52
  • @Someprogrammerdude I am sure I made mistake in my dp table , i am trying to correct it ,but can you help with that? – 12341c0des Jan 20 '19 at 10:08
  • My solution is picking the largest sum in a given query but it should be largest as well as contiguous @Someprogrammerdude – 12341c0des Jan 20 '19 at 10:10

0 Answers0