简体   繁体   中英

How Vector allocation using push_back() and pop_back operation gives garbage values

I am solving a problem in which I have to find those element from the array whose total gives maximum sum. But there is a condition that no two adjacent element can be the part of that max subarray. Here is my code using simple brute Force solution-

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t != 0)
    {
        int n, i, s, k = 0, m = -1001;
        vector< int > a;
        cin >> n;
        a.resize(n, 0);
        vector< int > b;
        for (i = 0; i < n; i++)
        {
            cin >> a[i];
            m = max(m, a[i]);
            if (a[i] < 0)
            {
                a[i] = 0;
                ++k;
            }
        }
        if (k == n)
            cout << m;
        else
        {
            k = 0;
            s = a[0];
            b.push_back(a[0]);
            for (i = 1; i < n; i++)
            {
                if (i != k + 1)
                {
                    if (a[i])
                    {
                        s += a[i];
                        b.push_back(a[i]);
                        k = i;
                    }
                }
                else
                {
                    if (s - a[i - 1] + a[i] > s)
                    {
                        b.pop_back();
                        s -= a[i - 1];
                        s += a[i];

                        b.push_back(a[i]);

                        ++k;
                    }
                }
            }
        }
        cout << endl;
        for (i = n; i >= 0; i--)
        {
            if (b[i])
                cout << b[i] << " ";
        }
        cout << endl;
        --t;
    }
    return 0;
}

Here is input to code- First line represent no. of test cases, Second line represent size of array And the next line shows array elements.m

5
5
-1 7 8 -5 4 
4
3 2 1 -1 
4
11 12 -2 -1 
4
4 5 4 3 
4
5 10 4 -1

Output-

4 8 
32 32607 -787829912 1 3 
32 32607 -787829912 12 
3 5 
10 

Expected output-

4 8
1 3
12
3 5
10

So, there are 5 test cases. For the first test case and last two test case output is correct. But for second and third test case it is giving garbage value. What is the problem, that for some test cases it is giving garbage value, and for other not.

    for (i = n; i >= 0; i--)
    {
        if (b[i])
            cout << b[i] << " ";
    }

This prints out n+1 values in b . But even in the best case, b only has n values (for n=1 ). And for n>1 , b.size() is less than n , so you are reading garbage from outside the vector's storage (this is undefined behavior ). Just use the correct bound:

for (i = b.size() - 1; i >= 0; ++i)

I think I found your (first) problem:

if(k==n)
cout<<m;

When all numbers are negative this outputs the largest of them.

But the empty array has a sum of 0 and is larger than a negative number and has no 2 adjacent members in it. So clearly the right answer should be 0, not m.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM