https://www.geeksforgeeks.org/minimum-product-subset-array/

Given an array a, we have to find minimum product possible with the subset of elements present in the array. The minimum product can be single element also.

Examples:

Input : a[] = { -1, -1, -2, 4, 3 }
Output : -24
Explanation : Minimum product will be ( -2 -1 -1 4 3 ) = -24

Input : a[] = { -1, 0 }
Output : -1
Explanation : -1(single element) is minimum product possible

Input : a[] = { 0, 0, 0 }
Output : 0

  1. 如果有偶数个负数且没有零,则结果为除最大负数之外的所有值的乘积。
  2. 如果有奇数个负数而没有零,那么结果就是所有乘积。
  3. 如果只有零和正,没有负,则结果为0。例外情况是,当没有负数且所有其他元素为正时,我们的结果应为第一个最小正数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int max_neg=-INF,min_pos=INF,count_neg=0,count_zero=0,prod=1,t,n;
int main(){
freopen("in","r",stdin);
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&t);
if(t==0) {
count_zero++;
continue;
}
if(t<0) {
count_neg++;
max_neg = max(max_neg, t);
}
if(t>0) min_pos = min(min_pos, t);
prod*=t;
}
if (count_zero == n ||(count_neg == 0 && count_zero > 0))
printf("0");
else if (count_neg == 0)
printf("%d",min_pos);
else if (!(count_neg & 1) && count_neg != 0)
printf("%d",prod / max_neg);
else printf("%d",prod);
return 0;
}