https://www.codechef.com/viewsolution/1059091451
Could someone figure out for what testcase my code is failing? I’ve tried every edge case I could think of. I think it might be failing for large inputs but not sure why?
https://www.codechef.com/viewsolution/1059091451
Could someone figure out for what testcase my code is failing? I’ve tried every edge case I could think of. I think it might be failing for large inputs but not sure why?
@cborg
here plzz refer my c++ code for better understanding of the logic .
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
long long int n;
cin>>n;
long long int a[n];
long long int ch=0;
map<long long int,long long int> mp;
for(long long int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]%2==0)
ch=i+1;
mp[i]=ch;
}
long long int ans=0,fans=0,cnt=0,cnt1=0;
for(long long int i=0;i<n;i++)
{
if(a[i]%2==0)
{
ans+=(i+1LL);
}
else
ans+=mp[i];
}
fans=ans;
for(long long int i=n-1;i>=0;i--)
{
if(a[i]%2)
{
cnt++;
cnt1+=mp[i];
long long int tmans=ans-cnt1;
tmans=tmans+((i+1)*cnt);
fans=max(fans,tmans);
}
else
{
cnt=0;
cnt1=0;
}
}
cout<<fans<<endl;
}
return 0;
}