Make it divisible problem code ALLDIV3

Can anyone help me out with this problem…
I want to know the test case where my code is failing??
My code is as below…

import java.util.Scanner;
public class Main
{
    public static void main (String[] args)
    {
        Scanner s=new Scanner(System.in);
        int t=s.nextInt();
        for(int i=0;i<t;i++)
        {
            int n = s.nextInt();
            int twos=0;
            int ones=0;
            int zeros=0;
            int steps=0;
            int arr[]=new int[n];
            for(int j=0;j<n;j++)
            {
                arr[j]=s.nextInt();
                arr[j]=arr[j]%3;
                if(arr[j]==1)
                {
                    ones++;
                }
                else if(arr[j]==2)
                {
                    twos++;
                }
                else
                {
                    zeros++;
                }
            }
            if(ones==0 && twos==0)
            {
                steps=0;
            }
            else if(ones==twos)
            {
                steps=ones;
            }
            else
            {
                if(ones>twos)
                {
                    steps=steps+twos;
                    zeros=zeros+(twos);
                    ones=ones-twos;
                    twos=0;
                    if(ones%3==0)
                    {
                        steps=steps+(ones-1);
                    }
                    else
                    {
                        steps=-1;
                    }
                }
                else if(ones<twos)
                {
                    steps=steps+ones;
                    zeros=zeros+ones;
                    twos=twos-ones;
                    ones=0;
                    if(twos%3==0)
                    {
                        steps=steps+((twos/3)*2);
                    }
                    else
                    {
                        steps=-1;
                    }
                }
            }
            System.out.println(steps);
        }
    }
}

CodeChef: Practical coding for everyone - see this, just changed a single line in your code and it got accepted,
for more optimized approach, you can refer to CodeChef: Practical coding for everyone.

1 Like

Yaa understood…Thank u so much

1 Like