0

I was solving subarray with given sum,Where we have to print the starting and ending index of array if subarray with sum is found , when I tried with two test cases simultaneously i got wrong result But when I was tried one at a time I got right answer in both. You please also check in your IDE this is happening in every IDE.

Testcase (Simultaneously)

2

5 12

1 2 3 7 5

10 15

1 2 3 4 5 6 7 8 9 10

Output

2 4 (expected 2 4)

2 5 (But expected 1 5)

But when I tried like this for second test cases

1

10 15

1 2 3 4 5 6 7 8 9 10

Output : 1 5(As expected)

I got correct answer ,why my program this kind of weird behaviour ?

#include<iostream>
#include<vector>
#include<queue>
#include<unordered_map>
using namespace std;
vector<int>a;
unordered_map<int, int>seen;
int main()
{
    int t;
    cin >> t;
    while (t--) {
        int n, s;
        cin >> n >> s;
        a.resize(n);
        int sum = 0;
        seen[0] = -1;
        for (int i = 0; i < n; i++) {
            cin >> a[i];
            sum += a[i];
            if (seen.find(sum - s) != seen.end()) {
                int x;
                x = seen[sum - s] + 2;
                cout << x << " " << i + 1 << endl;
                break;
            }
            else {
                seen[sum] = i;
            }
        }


        seen.clear();
        a.clear();
        //cout<<endl;
    }
    return 0;
}
akacodes121
  • 129
  • 8
  • 3
    This is a big chunk of completely uncommented code with not even a hint as to what all those numbers mean or what it's supposed to do. The impenetrably vague variable names don't help either. – tadman Oct 02 '19 at 17:23
  • 2
    Time to learn how to use a debugger to step through your code statement by statement, while monitoring variables and their values. I also recommend you stop going to online competition/judge sites to learn basic programming, because they won't teach you that or how to write good code. Take a class or a course, or [read some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Oct 02 '19 at 17:25
  • I used debugger randomly garbage value was getting stored in n,I don't know why it is happening ? – akacodes121 Oct 02 '19 at 17:27
  • 1
    @akacodes121 -- Stop writing this "online judge" style of sticking everything in `main`. You should put the solution, whatever variables it needs in a *separate function*, and not stick everything in `main`. The lack of comments also is bad. And last -- if you know the test data that fails, do not post code with `cin` statements and similar input statements. Just set the variables to the test data directly. No one is going to sit at their computer retyping in test data every time they want to run the program. – PaulMcKenzie Oct 02 '19 at 17:43
  • Set a breakpoint here: `cin >> n >> s;` and look at the value of `n` if you say it takes on random garbage values. That seems like the only place `n` is set. – drescherjm Oct 02 '19 at 17:47
  • This looks like a case of placing everything in `main` and polluting one test case's information with the next. That's why the suggestion of writing a function should be heeded. No one writes tests like this -- tests are meant to be placed in functions so that all variables, effects, etc. are isolated between tests. – PaulMcKenzie Oct 02 '19 at 17:50
  • 1
    @akacodes121 -- [This is what you should have written](https://coliru.stacked-crooked.com/a/1c2254674240709b). Note the function is used. Second, the results of the isolated test shows the first input you said didn't work. Last, a trick that you may not be familiar with, but putting the test in a `std::string` and using `std::istringstream` can be substituted for having to type in the test data. – PaulMcKenzie Oct 02 '19 at 18:03

0 Answers0