Codeforces 1206B Make Product Equal One Solution
Solve in C++:
///AH Tonmoy
///Department of CSE
///Islamic University,Bangladesh
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int ar[100001]= {0},n,i,o=0,sn=0,sp=0,s=0,ne=0,pos=0,r=0;
cin>>n;
{
for(i=0; i<n; i++)
{
cin>>ar[i];
if(ar[i]>0)
{
s=s+abs(ar[i])-1;
pos++;
}
else if(ar[i]<0)
{
s=s+(abs(ar[i])-1);
ne++;
}
else if(ar[i]==0)
{
s=s+1;
o++;
}
}
if((o==0)&&(ne%2!=0))
cout<<s+2<<endl;
else
cout<<s<<endl;
}
}
///AH Tonmoy
///Department of CSE
///Islamic University,Bangladesh
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int ar[100001]= {0},n,i,o=0,sn=0,sp=0,s=0,ne=0,pos=0,r=0;
cin>>n;
{
for(i=0; i<n; i++)
{
cin>>ar[i];
if(ar[i]>0)
{
s=s+abs(ar[i])-1;
pos++;
}
else if(ar[i]<0)
{
s=s+(abs(ar[i])-1);
ne++;
}
else if(ar[i]==0)
{
s=s+1;
o++;
}
}
if((o==0)&&(ne%2!=0))
cout<<s+2<<endl;
else
cout<<s<<endl;
}
}
No comments