Problem p02616 (C++) — buggy submission, verdict: Time Limit Exceeded

// author rhythmicankur
// god_is_almighty
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
#define f first
#define s second
#define int long long
#define ll int
#define ld long double
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define vi vector<int>
#define pii pair<int, int>
#define pip pair<int, pii>
#define vpi vector<pii>
#define sbcount(x) __builtin_popcountll(x)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define fill(a, val) memset(a, val, sizeof(a))
#define PI acos(-1)
#define coud(a, b) cout << fixed << setprecision((b)) << (a)
#define M1 998244353
#define M2 1000000007
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;

template <typename T> T gcd(T a, T b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
#define test4(x, y, z, a)                                                      \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << "  |  " << #a << ": " << a << endl;
#define test1(x) cerr << #x << ": " << x << endl
#define test2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define test3(x, y, z)                                                         \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
ll power(ll a, ll b, ll m = M2) {
  ll answer = 1;
  while (b) {
    if (b & 1)
      answer = (answer * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return answer;
}
using namespace std;
void ctrl() { cout << "Control" << endl; }
int make_num(string p) {
  stringstream geek(p);
  int x = 0;
  geek >> x;
  return x;
}
string make_str(int x) {
  ostringstream str1;
  str1 << x;
  string geek = str1.str();
  return geek;
}
int adjust(int x) {
  if (x > 0)
    return x % M2;

  int y = -x;
  int h = (y / M2);
  h += 3;
  x += (h * M2);
  return x % M2;
}
int maxProductSubarrayOfSizeK(int A[], int n, int k) {
  // sorting given input array
  sort(A, A + n);

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

  if (A[n - 1] == 0 && (k & 1))
    return 0;

  if (A[n - 1] <= 0 && (k & 1)) {
    for (int i = n - 1; i >= n - k; i--) {
      product *= A[i];
      product = adjust(product);
      product %= M2;
    }

    return product;
  }

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

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

  if (k & 1) {
    product *= A[j];
    product = adjust(product);
    product %= M2;
    j--;
    k--;
  }

  k >>= 1;

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

    // test1(product);

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

    int right_product = A[j] * A[j - 1];

    if (left_product > right_product) {
      left_product %= M2;
      left_product = adjust(left_product);
      product *= left_product;
      product %= M2;

      product = adjust(product);

      i += 2;
    } else {
      right_product %= M2;

      right_product = adjust(right_product);
      product *= right_product;
      product %= M2;

      product = adjust(product);

      j -= 2;
    }

    product = adjust(product);
  }

  // Finally return product
  return product % M2;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int n, k;
  cin >> n >> k;

  int A[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];

  /*int s=1;
  for(int i=0;i<n;i++)
  s = (s*A[i])%M2;
  test1(s);*/

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

  return 0;
}

Error: TLE

Fixed submission:

// author rhythmicankur
// god_is_almighty
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
#define f first
#define s second
#define int long long
#define ll int
#define ld long double
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define vi vector<int>
#define pii pair<int, int>
#define pip pair<int, pii>
#define vpi vector<pii>
#define sbcount(x) __builtin_popcountll(x)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define fill(a, val) memset(a, val, sizeof(a))
#define PI acos(-1)
#define coud(a, b) cout << fixed << setprecision((b)) << (a)
#define M1 998244353
#define M2 1000000007
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;

template <typename T> T gcd(T a, T b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
#define test4(x, y, z, a)                                                      \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << "  |  " << #a << ": " << a << endl;
#define test1(x) cerr << #x << ": " << x << endl
#define test2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define test3(x, y, z)                                                         \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
ll power(ll a, ll b, ll m = M2) {
  ll answer = 1;
  while (b) {
    if (b & 1)
      answer = (answer * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return answer;
}
using namespace std;
void ctrl() { cout << "Control" << endl; }
int make_num(string p) {
  stringstream geek(p);
  int x = 0;
  geek >> x;
  return x;
}
string make_str(int x) {
  ostringstream str1;
  str1 << x;
  string geek = str1.str();
  return geek;
}
int maxProductSubarrayOfSizeK(int A[], int n, int k) {
  // sorting given input array
  sort(A, A + n);

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

  if (A[n - 1] == 0 && (k & 1))
    return 0;

  if (A[n - 1] <= 0 && (k & 1)) {
    for (int i = n - 1; i >= n - k; i--) {
      product *= A[i];
      while (product < 0)
        product += M2;
      product %= M2;
    }

    return product;
  }

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

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

  if (k & 1) {
    product *= A[j];
    product %= M2;
    j--;
    k--;
  }

  k >>= 1;

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

    // test1(product);

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

    int right_product = A[j] * A[j - 1];

    if (left_product > right_product) {
      left_product %= M2;
      while (left_product < 0)
        left_product += M2;
      product *= left_product;
      product %= M2;

      while (product < 0)
        product += M2;

      i += 2;
    } else {
      right_product %= M2;

      while (right_product < 0)
        right_product += M2;
      product *= right_product;
      product %= M2;

      while (product < 0)
        product += M2;

      j -= 2;
    }

    while (product < 0)
      product += M2;
  }

  // Finally return product
  return product % M2;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int n, k;
  cin >> n >> k;

  int A[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];

  /*int s=1;
  for(int i=0;i<n;i++)
  s = (s*A[i])%M2;
  test1(s);*/

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

  return 0;
}