Language: Java

//g++  7.4.0

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    ll T = 1;
    while(T--)
    {
        ll N,K; cin>>N>>K;
        ll arr[N];
        ll ans = 1;
        
        for(ll i=0;i<N;++i)
            cin>>arr[i];
        
        sort(arr,arr + N);
        
        ll pos = 0;
        ll neg = 0;
        ll zero = 0;
        ll pos_start = -1;
        ll pos_end = -1;
        ll neg_start = -1;
        ll neg_end = -1;
        
        for(ll i=0;i<N;++i)
        {
            if(arr[i] >= 0)
            {
                if(pos_start == -1)
                {
                    pos_start = i;
                    pos_end = i;
                }
                else
                    pos_end = i;
                
                pos++;
            }
            else if(arr[i] < 0)
            {
                if(neg_start == -1)
                {
                    neg_start = i;
                    neg_end = i;
                }
                else
                    neg_end = i;
                
                neg++;
            }
        
        }
        
        if(pos == 0)
        {
            if(K%2)
            {
                //cout<<"Hello"<<endl;
                for(ll i=neg_end;i>=(neg_end - K + 1);--i)
                {
                    //cout<<arr[i]<<" ";
                    ans = ((ans % MOD) * (((arr[i] % MOD) + MOD) % MOD)) % MOD;
                    //cout<<ans<<endl;
                }
            }
            else
            {
                for(ll i=0;i<K;++i)
                    ans = ((ans % MOD) * (((arr[i] % MOD) + MOD) % MOD)) % MOD;
            }
            
            cout<<ans<<endl;
            continue;
        }
        
        /*if(neg == 0)
        {
            for(ll i=(N - 1);i>(N - K);--i) //to check
                ans = (ans * arr[i]) % MOD;
                
            cout<<ans<<endl;
            
            continue;
        }*/
        
        ll i = 0;
        ll j = N - 1;
        
        while((K > 1) && (i < neg_end) && (j > pos_start))
        {
            //cout<<i<<endl;
            
            ll fwd = arr[i] * arr[i + 1];
            ll bwd = arr[j] * arr[j - 1];
            
            if(fwd > bwd)
            {
                i += 2;
                ans = ((ans % MOD) * (fwd % MOD)) % MOD;
                K -= 2;
            }
            else
            {
                ans = ((ans % MOD) * (arr[j] % MOD)) % MOD;
                j--;
                K--;
            }
            
        }
        
        //cout<<ans<<endl;
        
        //cout<<K<<" "<<i<<" "<<j<<endl;
        
        while((K > 1) && (i < neg_end))
        {
            ll fwd = arr[i] * arr[i + 1];
            //cout<<fwd<<endl;
            i += 2;
            ans = ((ans % MOD) * (fwd % MOD)) % MOD;
            K -= 2;
        }
        
        //cout<<K<<endl;
        
        while(K && (j >= pos_start))
        {
            ans = ((ans % MOD) * (arr[j] % MOD)) % MOD;
            j--;
            K--;
        }
        
        //cout<<ans<<endl;
        if(K)//edge case
        {
            ans = ((ans % MOD) * (((arr[i] % MOD) + MOD) % MOD)) % MOD;
        }
        
        cout<<ans<<endl;
    }
}

Compiler output:
Main.java:3: error: illegal character: '#'
#include <iostream>
^
Main.java:4: error: illegal character: '#'
#include <bits/stdc++.h>
^
Main.java:6: error: class, interface, enum, or record expected
typedef long long ll;
^
Main.java:7: error: illegal character: '#'
#define MOD 1000000007
^
Main.java:7: error: class, interface, enum, or record expected
#define MOD 1000000007
        ^
Main.java:12: error: class, interface, enum, or record expected
    cin.tie(NULL);
    ^
Main.java:14: error: unnamed classes are a preview feature and are disabled by default.
    ll T = 1;
    ^
  (use --enable-preview to enable unnamed classes)
Main.java:15: error: class, interface, enum, or record expected
    while(T--)
    ^
Main.java:17: error: class, interface, enum, or record expected
        ll N,K; cin>>N>>K;
                ^
Main.java:18: error: class, interface, enum, or record expected
        ll arr[N];
        ^
Main.java:21: error: class, interface, enum, or record expected
        for(ll i=0;i<N;++i)
        ^
Main.java:21: error: class, interface, enum, or record expected
        for(ll i=0;i<N;++i)
                   ^
Main.java:21: error: class, interface, enum, or record expected
        for(ll i=0;i<N;++i)
                       ^
Main.java:24: error: class, interface, enum, or record expected
        sort(arr,arr + N);
        ^
Main.java:34: error: class, interface, enum, or record expected
        for(ll i=0;i<N;++i)
        ^
Main.java:34: error: class, interface, enum, or record expected
        for(ll i=0;i<N;++i)
                   ^
Main.java:34: error: class, interface, enum, or record expected
        for(ll i=0;i<N;++i)
                       ^
Main.java:41: error: class, interface, enum, or record expected
                    pos_end = i;
                    ^
Main.java:42: error: class, interface, enum, or record expected
                }
                ^
Main.java:46: error: class, interface, enum, or record expected
                pos++;
                ^
Main.java:47: error: class, interface, enum, or record expected
            }
            ^
Main.java:53: error: class, interface, enum, or record expected
                    neg_end = i;
                    ^
Main.java:54: error: class, interface, enum, or record expected
                }
                ^
Main.java:58: error: class, interface, enum, or record expected
                neg++;
                ^
Main.java:59: error: class, interface, enum, or record expected
            }
            ^
Main.java:68: error: class, interface, enum, or record expected
                for(ll i=neg_end;i>=(neg_end - K + 1);--i)
                                 ^
Main.java:68: error: class, interface, enum, or record expected
                for(ll i=neg_end;i>=(neg_end - K + 1);--i)
                                                      ^
Main.java:73: error: class, interface, enum, or record expected
                }
                ^
Main.java:77: error: class, interface, enum, or record expected
                for(ll i=0;i<K;++i)
                           ^
Main.java:77: error: class, interface, enum, or record expected
                for(ll i=0;i<K;++i)
                               ^
Main.java:79: error: class, interface, enum, or record expected
            }
            ^
Main.java:82: error: class, interface, enum, or record expected
            continue;
            ^
Main.java:83: error: class, interface, enum, or record expected
        }
        ^
Main.java:98: error: class, interface, enum, or record expected
        while((K > 1) && (i < neg_end) && (j > pos_start))
        ^
Main.java:105: error: class, interface, enum, or record expected
            if(fwd > bwd)
            ^
Main.java:108: error: class, interface, enum, or record expected
                ans = ((ans % MOD) * (fwd % MOD)) % MOD;
                ^
Main.java:109: error: class, interface, enum, or record expected
                K -= 2;
                ^
Main.java:110: error: class, interface, enum, or record expected
            }
            ^
Main.java:114: error: class, interface, enum, or record expected
                j--;
                ^
Main.java:115: error: class, interface, enum, or record expected
                K--;
                ^
Main.java:116: error: class, interface, enum, or record expected
            }
            ^
Main.java:128: error: class, interface, enum, or record expected
            i += 2;
            ^
Main.java:129: error: class, interface, enum, or record expected
            ans = ((ans % MOD) * (fwd % MOD)) % MOD;
            ^
Main.java:130: error: class, interface, enum, or record expected
            K -= 2;
            ^
Main.java:131: error: class, interface, enum, or record expected
        }
        ^
Main.java:138: error: class, interface, enum, or record expected
            j--;
            ^
Main.java:139: error: class, interface, enum, or record expected
            K--;
            ^
Main.java:140: error: class, interface, enum, or record expected
        }
        ^
Main.java:146: error: class, interface, enum, or record expected
        }
        ^
Main.java:149: error: class, interface, enum, or record expected
    }
    ^
50 errors