Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <chrono>
#include <map>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include <vector>

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repu(i, k, n) for (ll i = k; i <= n; i++)
#define repd(i, k, n) for (ll i = k; i >= n; i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define endl "\\n"
#define INF (ll) ll_MAX

typedef long long ll;
typedef long double ld;
typedef std::pair<ll, ll> pii;
typedef std::vector<ll> vi;
typedef std::vector<pii> vii;
typedef std::vector<ll> vll;
typedef std::vector<std::pair<pii, ll>> vpp;

const ll N = 3 * 1e5 + 5;
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
const long long inf = 1e18;

using namespace std;

//   cin>>t;

ll maxProductSubarrayOfSizeK(ll A[], ll n, ll k) {
  sort(A, A + n);

  // variable to store final product of all element
  // of sub-sequence of size k
  ll product = 1;

  // CASE I
  // If max element is 0 and
  // k is odd then max product will be 0
  if (A[n - 1] == 0 && (k & 1))
    return 0;

  // CASE II
  // If all elements are negative and
  // k is odd then max product will be
  // product of rightmost-subarray of size k
  if (A[n - 1] <= 0 && (k & 1)) {
    for (int i = n - 1; i >= n - k; i--) {
      product *= (A[i] % MOD);
      product %= MOD;
    }
    if (product < 0)
      product += MOD;
    return product;
  }

  // else
  // i is current left pointer index
  ll i = 0;

  // j is current right pointer index
  ll j = n - 1;

  // CASE III
  // if k is odd and rightmost element in
  // sorted array is positive then it
  // must come in subsequence
  // Multiplying A[j] with product and
  // correspondingly changing j
  if (k & 1) {
    product *= (A[j] % MOD);
    product %= MOD;
    j--;
    k--;
  }

  // CASE IV
  // Now k is even
  // Now we deal with pairs
  // Each time a pair is multiplied to product
  // ie.. two elements are added to subsequence each time
  // Effectively k becomes half
  // Hence, k >>= 1 means k /= 2
  k >>= 1;

  // Now finding k corresponding pairs
  // to get maximum possible value of product
  for (ll itr = 0; itr < k; itr++) {

    // product from left pointers
    ll left_product = A[i] * A[i + 1];

    // product from right pointers
    ll right_product = A[j] * A[j - 1];

    // Taking the max product from two choices
    // Correspondingly changing the pointer's position
    if (left_product > right_product) {
      product *= (left_product % MOD);
      product %= MOD;
      i += 2;
    } else {
      product *= (right_product % MOD);
      product %= MOD;
      j -= 2;
    }
  }

  // Finally return product
  product = product % MOD;
  if (product < 0)
    product += MOD;
  return product % MOD;
}

ll modulo = 1000000007;

void solve() {

  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (ll i = 0; i < n; i++)
    cin >> a[i];

  cout << maxProductSubarrayOfSizeK(a, n, k);

  // vll pos,neg;
  // for(ll i=0;i<n;i++){
  //     if(a[i]<0)neg.pb(-a[i]);
  //     else if(a[i]>0) pos.pb(a[i]);
  // }

  // if(neg.size()+pos.size()<k){
  //     cout<<0;
  //     return;
  // }
  // ll is0 = 0;
  // if(neg.size()+pos.size()<n){
  //     is0 = 1;
  // }

  // sort(neg.begin(),neg.end());
  // sort(pos.begin(),pos.end());

  // ll ans = 1;

  // if(pos.size()==0 and is0==0 and k%2==1){
  //     for(ll i=0;i<k;i++){
  //         ans*= -neg[i];
  //         ans%= 1000000007;
  //     }
  //     if(ans<0) ans+= 1000000007;
  //     cout<<ans;
  //     return;
  // }

  // if(pos.size()==0 and is0==1 and k%2==1){
  //     cout<<0;
  //     return;
  // }

  // else{
  //     reverse(neg.begin(),neg.end());
  //     reverse(pos.begin(),pos.end());

  //     ll ctrn = 0;
  //     ll ctrp = 0;

  //     while(ctrn<neg.size()-1 and ctrp<pos.size() and k>1){
  //         if(neg[ctrn]*neg[ctrn+1] > pos[ctrp]*pos[ctrp+1]){
  //             ans*=neg[ctrn];
  //             ans%= 1000000007;
  //             ans*=neg[ctrn+1];
  //             ans%= 1000000007;
  //             ctrn+=2;
  //             k-=2;
  //         }
  //         else{
  //             ans*=pos[ctrp];
  //             ans%= 1000000007;
  //             ctrp++;
  //             k--;
  //         }
  //     }
  //     while(k>0){
  //         ans*=pos[ctrp];
  //         ans%= 1000000007;
  //         ctrp++;
  //         k--;
  //     }
  //     cout<<ans;

  // }
}

int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  // cout<<"helo";
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;

  while (t--) {
    solve();
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <chrono>
#include <map>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include <vector>

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repu(i, k, n) for (ll i = k; i <= n; i++)
#define repd(i, k, n) for (ll i = k; i >= n; i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define endl "\\n"
#define INF (ll) ll_MAX

typedef long long ll;
typedef long double ld;
typedef std::pair<ll, ll> pii;
typedef std::vector<ll> vi;
typedef std::vector<pii> vii;
typedef std::vector<ll> vll;
typedef std::vector<std::pair<pii, ll>> vpp;

const ll N = 3 * 1e5 + 5;
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
const long long inf = 1e18;

using namespace std;

//   cin>>t;

bool isPrime(ll n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  // This is checked so that we can skip
  // middle five numbers in below loop
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (ll i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}

ll modulo = 1000000007;

void solve() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (ll i = 0; i < n; i++)
    cin >> a[i];

  vll pos, neg;
  for (ll i = 0; i < n; i++) {
    if (a[i] < 0)
      neg.pb(-a[i]);
    else if (a[i] > 0)
      pos.pb(a[i]);
  }

  if (neg.size() + pos.size() < k) {
    cout << 0;
    return;
  }
  ll is0 = 0;
  if (neg.size() + pos.size() < n) {
    is0 = 1;
  }

  sort(neg.begin(), neg.end());
  sort(pos.begin(), pos.end());

  ll ans = 1;

  if (pos.size() == 0 and is0 == 0 and k % 2 == 1) {
    for (ll i = 0; i < k; i++) {
      ans *= -neg[i];
      ans %= 1000000007;
    }
    if (ans < 0)
      ans += 1000000007;
    cout << ans;
    return;
  }

  if (pos.size() == 0 and is0 == 1 and k % 2 == 1) {
    cout << 0;
    return;
  }

  else {
    reverse(neg.begin(), neg.end());
    reverse(pos.begin(), pos.end());

    ll ctrn = 0;
    ll ctrp = 0;

    while (ctrn < neg.size() - 1 and ctrp < pos.size() and k > 1) {
      if (neg[ctrn] * neg[ctrn + 1] > pos[ctrp] * pos[ctrp + 1]) {
        ans *= neg[ctrn];
        ans %= 1000000007;
        ans *= neg[ctrn + 1];
        ans %= 1000000007;
        ctrn += 2;
        k -= 2;
      } else {
        ans *= pos[ctrp];
        ans %= 1000000007;
        ctrp++;
        k--;
      }
    }
    while (k > 0) {
      ans *= pos[ctrp];
      ans %= 1000000007;
      ctrp++;
      k--;
    }
    cout << ans;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  // cout<<"helo";
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;

  while (t--) {
    solve();
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}