I was trying to find product of some long long integer (0<=a[i]<=10^18)t. now how do i find that the product exeeds 10^18? does the product turn into negetive number (product<0)?
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
long long a[n],product=1;
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
for(auto i:a){
product*=i;
if(i==0) break;
if(product>1000000000000000000) {
cout<<-1;
return 0;
}
}
cout<<product;
}
In my code, I use function
isOverflow()to check it. You can search and go throughstd::numeric_limits<T>::max().Additionally, since the first comments mentioned several questions about C++ practice, so I made some changes on your code for a better C++ coding habit and convention. I explained the reason by comments.
According to the suggestion of comments, I have removed VLA(Variable Length Array).