1

My apologies, I know this type of question already has an answer over here but I couldn't figure out how to use it for my code. I wrote a program for a problem-solving contest that accepts an array and tries to maximize the value of |Ax−Ay|+|Ay−Az|+|Az−Ax| over all triples of pairwise distinct valid indices (x,y,z). The program has the following constraints:

  • 1≤t≤5
  • 3≤n≤10^5
  • |Ai|≤10^9 for each valid i

I am getting the following error when I try to run it - "terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc". All I could figure out from the answered questions was that my code encounters memory allocation problem but I couldn't find where and when it does that? Maybe when it deals with large values? What is causing the error?

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll> vll;
typedef vector<int> vi;

#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repi(i, a, b) for (ll i = a; i <= b; i++)
#define repn(i, a, b) for (ll i = a; i >= b; i--)
#define fast()                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
// solve() function
void solve()
{
    ll n;
    cin >> n;
    vll v(n);
    rep(i, 0, n)
            cin >>
        v[i];
    sort(all(v));
    ll x = v[0], y = v[1], z = v[n - 1];
    ll ans = abs(x - y) + abs(y - z) + abs(z - x);
    cout << ans << endl;
}
// driver function
int main()
{
    fast();
    ll t = 1;
    cin >> t;
    rep(i, 0, t)
    {
        solve();
    }
    return 0;
}

The input format is as follows:

Input
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N.
The second line contains N space-separated integers A1, A2,…,AN.

The following is a sample input:

3
3
2 7 5
3
3 3 3
5
2 2 2 2 5
Mohit Singh
  • 143
  • 11
  • may be you entered `n` that is very big but vector could not allocate that much memory, to reserve as much as you need you to do this `vll.reserve(n)`. it may solve your issue because when you enter `n` vector allocates more memory than need, depends on the compiler implementation. – foragerDev Feb 08 '21 at 12:27
  • 2
    Please deobfuscate your code if you want us to try and analyze it. You should also add the program's input and the exact point at which the exception occurs. A glaring issue I can see is the lack of input error checking: you sould double-check that `t` and especially `n` have sensible values. – Quentin Feb 08 '21 at 12:27
  • What are you doing? What's the problem with writing a simple loop? Why would you use defines! DONT. it is error prone, like you see in your code. – JHBonarius Feb 08 '21 at 12:51
  • @JHBonarius That is a part of a contest template. I use it only when I am participating in timed-contests to save time while writing code. I try to devote as much time as possible to the thinking process of the contest problem. This is not how I usually write code. – Mohit Singh Feb 08 '21 at 13:01
  • Only your first `ans` uses three *distinct* indices. The other two are equivalent to `2 * abs(v[1] - v[n-1])` and `2 * abs(v[0] - v[n-1])`, and the former can't be greater than the latter. (This is almost a trick question, as far as I can tell; I don't think you need either a vector or any sorting.) – molbdnilo Feb 08 '21 at 14:17
  • @molbdnilo Thank You. I understood your point and made changes to the code. As for your second argument about the need for a vector or sorting, I have added the input format provided with the problem as well as a sample input. – Mohit Singh Feb 08 '21 at 17:00
  • 1
    @MohitSingh Even your solution only uses the smallest, second smallest, and largest numbers. You can determine those from an arbitrarily large amount of inputs without storing more than three numbers. (And you actually only need two. Apply some algebra.) – molbdnilo Feb 09 '21 at 06:17
  • @molbdnilo Thanks! I understood your point and actually, only 2 numbers were required. I formulated a solution and it passed all test cases. Thank You again. – Mohit Singh Feb 10 '21 at 11:59

1 Answers1

1

you have to do something like this: I am not getting data via cin I am just specifying the value.

void solve()
{
    ll n =100000000; 
    vll v;
    v.reserve(n);
    //omitted
}

it runs fine,and it does not throw bad_alloc error. In your case, n may be uninitialized, and is not getting valid input so it pass n that is very big. when vll v(n) try to allocate it runs out of memory and returns 137 which means out of memory. so it fails. If you specify n directly into the constructor of vector it will allocate more memory(compiler dependent). But if you reserve the memory you need, it works fine until you have enough memory to hold the data you are putting in the vector.

foragerDev
  • 1,307
  • 1
  • 9
  • 22