# Help me in solving SEGTHREE problem

### My issue

Can anyone tell me which case I am missing or is this logic is correct or not.

### My code

``````/* package codechef; // don't place package name! */
/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
static long fun(long[]a , int n , long sum, long ans , int rem)
{
long minans = Integer.MAX_VALUE;
if(rem==0)
{
minans = fun2(a,n,sum,ans);
}
else if (rem==1)
{
a[0]++;
minans = fun2(a,n,sum+1,ans);
a[0]--;
a[1]++;
minans = Math.min(minans,fun2(a,n,sum+1,ans));
a[1]--;
a[2]++;
minans=Math.min(minans,fun2(a,n,sum+1,ans));
}
else if(rem==2)
{
a[0]++;a[1]++;
minans = fun2(a,n,sum+2,ans);
a[0]--;a[2]++;
minans = Math.min(minans,fun2(a,n,sum+2,ans));
a[1]--;a[0]++;
minans = Math.min(minans,fun2(a,n,sum+2,ans));
a[0]--;a[2]--;
a[2]+=2;
minans = Math.min(minans,fun2(a,n,sum+2,ans));
a[1]+=2;
minans = Math.min(minans,fun2(a,n,sum+2,ans));
a[0]+=2;
minans = Math.min(minans,fun2(a,n,sum+2,ans));

}
return minans;
}

static long fun2(long[]a , int n , long sum ,long ans)
{
long minans =ans, need =0;

for(int i =3 ; i<n;i++)
{
sum+=a[i];
sum-=a[i-3];
if(sum%3!=0)
{
need = 3 - (sum%3);
sum+=need;
a[i]+=need;
minans += need;
}
}
//System.out.print(minans+" ");
return minans;
}

public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t>0)
{
t--;
int n = sc.nextInt();
long[] a = new long[n] ;
long[] b = new long[n];
for(int i  =0 ; i<n ; i++ )
{
a[i] = sc.nextLong();
b[i] = a[i] ;
}

long minans =Integer.MAX_VALUE;
long ans = 0;
long sum = a[0]+a[1]+a[2] ;
System.out.print(sum);

if(sum%3==0)
{
minans=fun(a,n,sum,0,0 );
}
else if(sum%3==1)
{
minans = fun(a,n,sum,1,1);
}
else
{
minans = fun(a,n,sum,2,2);
}
System.out.println(minans);
}