HELP WITH PEPPERON

Could someone help me with the logic of PEPPERON ?
my code is:
import java.io.*;
class PEPPERON
{
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int T=Integer.parseInt(br.readLine());
int N,a[],j,i,k,min,co[][],x,y,c,d,p1,p2;
String s;
for(i=1;i<=T;i++)
{
p1=0;
p2=0;
N=Integer.parseInt(br.readLine());
//a=new int[M][N];
co=new int[N][2];
for(j=0;j<N;j++)
{
x=y=0;
s=br.readLine();
for(k=0;k<N;k++)
{
int z=s.charAt(k)-48;
if(z==1&&k<N/2)
{
x++;
p1++;
}
else if(z==1&&k>=N/2)
{
y++;
p2++;
}
co[k][0]=x;
co[k][1]=y;
}
}
min=(int)(Math.abs(p1-p2));
for(j=0;j<N;j++)
{
c=p1-co[j][0]+co[j][1];
d=p2-co[j][1]+co[j][0];
int e=(int)(Math.abs(c-d));
if(e<min)
min=e;

        }
        System.out.println(min);
    }
}

}

Help me with c++ also

I’ll tell you what to do in short

  1. First find the difference without flipping, lets call it ‘res’.
  2. Then for every row, make a variable ‘cur_res=res’ find the difference if it was flipped and change ‘cur_res’, and if ‘cur_res’ is less than ‘res’, update res with ‘cur_res’.
  3. At last you’ll have your answer.

I’ll share code tomorrow

1 Like

yes i did exactly in my pasted code…and yet it showed WA

https://www.codechef.com/viewsolution/25996418
see my python solution…
i created just two 2D arrays for both sides,
and checked if flipping the sides made p1-p2 less or not
if it did then i made it the new min… :slight_smile:

1 Like