Feedback for SEGTHREE problem

Problem Link: SEGTHREE Problem - CodeChef

Feedback

Sample test cases passed why solution is incorrect

@dshivank
Its because your logic is not right.
Plzz refer this solution for better understanding of the logic.

``````#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<algorithm>
#include<numeric>

using namespace std;
typedef long long int lli;
using namespace __gnu_pbds;
template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//typedef long long int lli;
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> multi_pbds;
//typedef tree<pair<int , int>, null_type, less<pair<int ,int>>, rb_tree_tag, tree_order_statistics_node_update> pbdsp;
//typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define nl "\n";
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define db long double
#define prq priority_queue<lli>
#define psq priority_queue<lli,vector<lli>,greater<lli>>
#define mod 1000000007
#define lb lower_bound
#define ub upper_bound
#define vlli vector<lli>
#define mslli multiset<lli>
#define inf 1e17
#define sp " "
#define pb push_back
#define pie 3.14159265358979323846
#define test lli t; cin>>t; while(t--)
void checkexist(lli n)
{
set<lli> s;
multiset<lli> s1;
for(lli i=0;i<n;i++)
{
lli sm=0;
sm+=i;
s.insert(sm);
if(!s1.count(sm))
{
s1.insert(sm);
}

}
}
void rev(string s , int id)
{
if(id==s.size())
{
return ;
}
rev(s,id+1);
cout<<s[id];
}
lli  cal(vector<lli>& v,int id)
{
if(id<0)
return 0;
lli fst,thrd,snd;
if(v[id]!=0)
{
if(id>=2)
{
fst=v[id]+cal(v,id-1);
snd=max({v[id],v[id-1]})+cal(v,id-2);
thrd=max({v[id],v[id-1],v[id-2]})+cal(v,id-3);
return min({fst,snd,thrd});
}
else if(id==1)
{
fst=v[id]+cal(v,id-1);
snd=max({v[id],v[id-1]})+cal(v,id-2);
return min(fst,snd);
}
else
{
return v[id]+cal(v,id-1);
}
}
else
{
return cal(v,id-1);
}
}
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
test{
int n;
cin>>n;
lli a1[n];
for(lli i=0;i<n;i++)
{
cin>>a1[i];
}
lli ans=INT_MAX;
for(int i=0;i<3;i++)
{

for(int j=0;j<3;j++)
{
lli a=a1[0]+i;
lli b=a1[1]+j;
lli c=a1[2];
lli tmans=i+j;
if((a+b+c)%3!=0)
{
tmans+=(3-(a+b+c)%3);
c+=(3-(a+b+c)%3);
}
a=b;
b=c;
for(int k=3;k<n;k++)
{
c=a1[k];
if((a+b+c)%3!=0)
{
tmans+=(3-(a+b+c)%3);
c+=(3-(a+b+c)%3);
}
a=b;
b=c;
}
// cout<<i<<" "<<j<<" "<<tmans<<endl;
ans=min(ans,tmans);
}

}
cout<<ans<<endl;
}
}
``````