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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define print(A, n)                                                            \\
  for (ll i = 0; i < n; ++i)                                                   \\
    cout << A[i] << ' ';                                                       \\
  cout << endl;
#define take(A, n)                                                             \\
  for (ll i = 0; i < n; ++i)                                                   \\
    cin >> A[i];
//    for(auto& it : m)
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
//  priority_queue<ll int> pq;
//  priority_queue <ll int, vector<ll int>, greater<ll int> > pq;
//  vector<ll int> v;
//  vector< pair<ll int,ll int> > v;
//  set<ll int>::iterator it=s.begin();
//  unordered_map<  pair<ll int,ll int>, ll int> mp;

ll int power(ll int x, ll int y, ll int p) {
  ll int res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  if (x == 0)
    return 0; // In case x is divisible by p;

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

void computeLPSArray(string pat, int M, int *lps) {
  // length of the previous longest prefix suffix
  int len = 0;

  lps[0] = 0; // lps[0] is always 0

  // the loop calculates lps[i] for i = 1 to M-1
  int i = 1;
  while (i < M) {
    if (pat[i] == pat[len]) {
      len++;
      lps[i] = len;
      i++;
    } else // (pat[i] != pat[len])
    {
      // This is tricky. Consider the example.
      // AAACAAAA and i = 7. The idea is similar
      // to search step.
      if (len != 0) {
        len = lps[len - 1];

        // Also, note that we do not increment
        // i here
      } else // if (len == 0)
      {
        lps[i] = 0;
        i++;
      }
    }
  }
}
bool subset[10005][1005];
void isSubsetSum(ll int set[], ll int n, ll int sum) {
  // The value of subset[i][j] will be true if
  // there is a subset of set[0..j-1] with sum
  // equal to i

  // If sum is 0, then answer is true

  // If sum is not 0 and set is empty,
  // then answer is false

  for (int i = 0; i <= n; i++)
    subset[i][0] = true;
  for (int i = 1; i <= sum; i++)
    subset[0][i] = false;
  // Fill the subset table in botton up manner
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= sum; j++) {
      if (j < set[i - 1])
        subset[i][j] = subset[i - 1][j];
      if (j >= set[i - 1])
        subset[i][j] = subset[i - 1][j] || subset[i - 1][j - set[i - 1]];
    }
  }

  /*   // uncomment this code to print table
   for (int i = 0; i <= n; i++)
   {
     for (int j = 0; j <= sum; j++)
        printf ("%4d", subset[i][j]);
     printf("\\n");
   }*/
}
ll int maxProductSubarrayOfSizeK(ll int A[], ll int n, ll int k) {
  // sorting given input array
  sort(A, A + n);

  // variable to store final product of all element
  // of sub-sequence of size k
  ll int 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 (ll int i = n - 1; i >= n - k; i--) {
      product *= A[i];
      product += MOD;
      product %= MOD;
    }
    return product;
  }

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

  // j is current right pointer index
  ll int 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];
    product += 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 int itr = 0; itr < k; itr++) {

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

    // product from right pointers
    ll int 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) {
      left_product += MOD;
      left_product %= MOD;
      product *= left_product;
      product += MOD;
      product %= MOD;
      i += 2;
    } else {
      right_product += MOD;
      right_product %= MOD;
      product *= right_product;
      product += MOD;
      product %= MOD;
      j -= 2;
    }
  }

  // Finally return product
  return product;
}

int main() {
  fastio();

  ll int T;
  T = 1;

  while (T--) {

    ll int n, m, k, i;
    cin >> n >> k;
    ll int a[n];
    for (i = 0; i < n; i++) {
      cin >> a[i];
    }
    cout << maxProductSubarrayOfSizeK(a, n, k);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define va vector<long long int> a
#define vb vector<long long int> b
const ll M = 1e9 + 7;
const ll inf = 1e18 + 5;
using namespace std;
void fast() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
}
ll bs(ll arr[], ll l, ll r, ll x) {
  ll mid = 0;
  if (r >= l) {
    if (arr[mid] == x)
      return mid;
    if (arr[mid] > x)
      return bs(arr, l, mid - 1, x);
    return bs(arr, mid + 1, r, x);
  }
  return -1;
}
ll power(ll x, ul y, ll p) {
  ll res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll findMaxProduct(ll arr[], ll n, ll k) {
  // Initialize the MaxProduct to 1, as all elements
  // in the array are positive
  ll MaxProduct = 1;
  for (ll i = 0; i < k; i++)
    MaxProduct = (MaxProduct * arr[i]) % M;

  int prev_product = MaxProduct % M;

  // Consider every product beginning with arr[i]
  // where i varies from 1 to n-k-1
  for (ll i = 1; i <= n - k; i++) {
    ll curr_product = ((prev_product / arr[i - 1]) * arr[i + k - 1]) % M;
    MaxProduct = max(MaxProduct, curr_product) % M;
    prev_product = curr_product % M;
  }

  // Return the maximum product found
  return MaxProduct % M;
}
void solve() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  if (findMaxProduct(a, n, k) < 0)
    cout << M + findMaxProduct(a, n, k) << "\\n";
  else
    cout << findMaxProduct(a, n, k) % M << "\\n";
}
int main() {
  fast();
  solve();
  return 0;
}