Language: C++

#include <algorithm> // for sorting
#include <iostream>
#define int long long
using namespace std;
#define mod 1000000007

// Required function
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;

    // 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 = (((product + mod) % mod) * ((A[i] + mod) % mod)) % mod;
        return product;
    }

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

    // j is current right pointer index
    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 = ((product + mod) % mod) * ((A[j] + mod) % mod) % 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 (int itr = 0; itr < k; itr++)
    {

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

        // product from right pointers
        int right_product = (A[j] * A[j - 1] + mod) % mod;
        // Taking the max product from two choices
        // Correspondingly changing the pointer's position
        if (left_product > right_product)
        {
            product = (((product + mod) % mod) * ((left_product + mod) % mod)) % mod;
            i += 2;
        }
        else
        {
            product = (((product + mod) % mod) * ((right_product + mod) % mod)) % mod;
            j -= 2;
        }
    }

    // Finally return product
    return (product + mod) % mod;
}

// Driver Code to test above function
int32_t main()
{
    int n, k;#include <algorithm> // for sorting
#include <iostream>
#define int long long
using namespace std;
#define mod 1000000007

// Required function
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;

    // 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 = (((product + mod) % mod) * ((A[i] + mod) % mod)) % mod;
        return product;
    }

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

    // j is current right pointer index
    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 = ((product + mod) % mod) * ((A[j] + mod) % mod) % 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 (int itr = 0; itr < k; itr++)
    {

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

        // product from right pointers
        int right_product = (A[j] * A[j - 1] + mod) % mod;
        // Taking the max product from two choices
        // Correspondingly changing the pointer's position
        if (left_product > right_product)
        {
            product = (((product + mod) % mod) * ((left_product + mod) % mod)) % mod;
            i += 2;
        }
        else
        {
            product = (((product + mod) % mod) * ((right_product + mod) % mod)) % mod;
            j -= 2;
        }
    }

    // Finally return product
    return (product + mod) % mod;
}

// Driver Code to test above function
int32_t main()
{
    int n, k;
    cin >> n >> k;
    int arr[n];
    for (int i = 0; i < n; i++)
        cin >> arr[i];
    cout << maxProductSubarrayOfSizeK(arr, n, k);

    return 0;
}
    cin >> n >> k;
    int arr[n];
    for (int i = 0; i < n; i++)
        cin >> arr[i];
    cout << maxProductSubarrayOfSizeK(arr, n, k);

    return 0;
}

Compiler output:
a.cc:94:14: error: stray '#' in program
   94 |     int n, k;#include <algorithm> // for sorting
      |              ^
a.cc: In function 'int32_t main()':
a.cc:94:15: error: 'include' was not declared in this scope
   94 |     int n, k;#include <algorithm> // for sorting
      |               ^~~~~~~
a.cc:94:24: error: 'algorithm' was not declared in this scope
   94 |     int n, k;#include <algorithm> // for sorting
      |                        ^~~~~~~~~
a.cc:97:1: error: expected primary-expression before 'using'
   97 | using namespace std;
      | ^~~~~
a.cc:102:1: error: a function-definition is not allowed here before '{' token
  102 | {
      | ^
a.cc:185:13: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
  185 | int32_t main()
      |             ^~
a.cc:185:13: note: remove parentheses to default-initialize a variable
  185 | int32_t main()
      |             ^~
      |             --
a.cc:185:13: note: or replace parentheses with braces to value-initialize a variable
a.cc:186:1: error: a function-definition is not allowed here before '{' token
  186 | {
      | ^