PEPPERON - Editorial

hey plz help i dont get where my code lags!!

#include<bits/stdc++.h>

using namespace std;

int main(){
//freopen(“input.txt”,“r”,stdin);
//freopen(“output.txt”,“w”,stdout);

ios::sync_with_stdio(0);
cin.tie(0);
int t;
scanf("%d",&t);
while(t--){
    int n;
    scanf("%d",&n);
    char matrix[n][n];
    // input matrix and calculate total sum
    int tsum=0,lsum=0;
    int minn=1000000000;
    for(int i=0;i<n;i++){
        int lrowsum=0,rrowsum=0;
        for(int j=0;j<n;j++){
            scanf("%c",&matrix[i][j]);
            int r=(int)matrix[i][j];
            tsum=tsum+r;
            

            if(j<n/2){
                lsum=lsum+r;
                lrowsum=lrowsum+r;
            }
            else{
                rrowsum=rrowsum+r;
            }
            
                minn=min(abs(2*lsum-tsum-2*abs(lrowsum+rrowsum)),minn);
            
        }
    }

    
    printf("%d\n",minn );

}

return 0;

}

it is giving wrong output !! but my logic maches as given in editorial! plz help i am trying from long time.

In all the loops change prefix operators to postfix operators.

If you don’t know what they are read this.

It is true that your logic is the same as given in the editorial but you are messing up with ascii values.

The ascii value of 0 is 48 and 1 is 49
Thus, r will either be 48 or 49 and not 0 or 1.

That’s your bug which I caught on first glance through he code.

I dont think there should be more :slight_smile:

but i typecast my value to int

That is the reason…
The character ‘0’ has an decimal value of 48 in the integer list.

If you want more explanation, see ASCII - Simple English Wikipedia, the free encyclopedia

Here, the character ‘0’ has decimal value 48 and character ‘1’ has decimal value 49

Problem:

@erfaniaa

int sum = 0;
		for(int i=0;i<n;i++)
			sum+=a[i];
		int ans = abs(sum);
		for(int i=0;i<n;i++)
		{
			ans = min(ans , abs(sum - 2*a[i]));
		}

Anybody explain me above part of Setter1 Solution.
for 1st Testcase: a[]=1,1,1,1,0,0
for 2nd Testcase: a[]=-2,2,1,-1

what happened after that?
Please, explain logic behind that :smiley:

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner k = new Scanner(System.in);
int n=k.nextInt();
while(n>0){
int totalstr=k.nextInt();
int arr[][]=new int[totalstr][totalstr];
int leftsum=0,rightsum=0;
k.nextLine();
for(int j=0;j<totalstr;j++){
String str=k.nextLine();

            for(int i=0;i<str.length();i++){
                if(i<str.length()/2){
                    if(str.charAt(i)=='1'){
                        leftsum++;
                        arr[j][i]=1;
                    }
                }else{
                    if(str.charAt(i)=='1'){
                        rightsum++;
                        arr[j][i]=1;
                    }
                }
            }
        }
        if(Math.abs(leftsum-rightsum)==0){
            System.out.println(0);
            return;
        }
        int min=1000;
       for(int a=0;a<arr.length;a++){
           int x=0,y=0;
            for(int b=0;b<arr.length;b++){
              if(arr[a][b]==1&&b<arr.length/2){
                  x++;
              }else if(arr[a][b]==1&&b>=arr.length/2){
                  y++;
                 
              }
            }
         int ans=Math.abs(leftsum-x+y-(rightsum-y+x));
         
          if(min>ans){
              min=ans;
          }
       }  
         System.out.print(min);
 n--;}
    
}}

what s the error is in my code

Here is the python code

whats happening here ? what does this sum - 2*a[i])); do ?

why in the solution
ans = min(ans , abs(sum - 2a[i])) is used why can’t we use
ans = sum(a) - 2
max(a)
@samarthtandon @ssrivastava990

1 Like

Because we need to check to each row, instead of just subtract the max…, @syntaxhacker @l_returns @ashokshaun
Pls help regarding this doubt , bcz why we have to check for each value, just subtract Max value, correct me if I m wrong.

please help me with my code i am not able to found error in my code , it is passing sample test case but giving wrong answer on submission
here is my code:

#include<bits/stdc++.h>

using namespace std;

int main()
{
int t;
cin>>t;
while(t–)
{
int n;
int ans;
cin>>n;
char pizza[n+1][n+1];
int countf[n+1];
int countl[n+1];
int maxdif=0;
int mindif;
long long int sumf=0,suml=0;
for(int i=1;i<=n;i++)
{

        for(int j=1;j<=n;j++)
        {
            cin>>pizza[i][j];
        }

    }
     for(int i=1;i<=n;i++)
    {
        countf[i]=0;
        for(int j=1;j<=n/2;j++)
        {
            if(pizza[i][j]=='1')
            {
                countf[i]++;
            }
        }
        sumf+=countf[i];
    }
    for(int i=1;i<=n;i++)
    {
        countl[i]=0;
        for(int j=(n/2)+1;j<=n;j++)
        {
            if(pizza[i][j]=='1')
            {
                countl[i]++;

            }

        }
        suml+=countl[i];
        if(abs(countl[i]-countf[i])>maxdif)
            {maxdif=abs(countl[i]-countf[i]);}
    }
    //cout<<sumf<<" "<<suml<<" ";

    mindif=abs(sumf-suml);
    if(mindif==0)
    {
        ans=mindif;
    }

  else
  {
      ans= mindif- (2*maxdif);
  }
if(ans>0)

{
    cout<<ans<<endl;
}
else
{
    cout<<mindif<<endl;
}

}


return 0;

}

Hi guys,
can anyone tell what’s wrong in this code?
I’m getting wrong answer
Thank you

	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner input = new Scanner(System.in);
		int test,rows,count,overallDiff,min;
		String pizzaGrid[] = new String[10000];
		int diffKeeper[] = new int[10000];
		test = input.nextInt();
		while(test-- > 0) {
		    overallDiff = 0;
		    rows = input.nextInt();
		    for (int i = 0; i < rows; i++) {
		        count = 0;
		        pizzaGrid[i] = input.next();
		        for (int j = 0; j < rows; j++) {
		            if (j < rows/2 && pizzaGrid[i].charAt(j) == '1') {
		                count++;
		            } else if (pizzaGrid[i].charAt(j) == '1') {
		                count--;
		            }
		        }
		        diffKeeper[i] = count*2;
		        overallDiff += count;
		    }
		    //System.out.println(overallDiff);
		    if (overallDiff != 0) {
		        min = overallDiff;
                for (int i = 0; i < rows; i++) {
                    if (Math.abs(overallDiff - diffKeeper[i]) < Math.abs(min)) {
                        min = Math.abs(overallDiff - diffKeeper[i]);
                    }
                }
                System.out.println(min);
		    } else {
		        System.out.println(0);
		    }
		}
	}
1 Like

#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define mp make_pair
#define endl “\n”
#define tab " "
#define pb push_back
#define F first
#define S second
#define watch(x) cout<<(#x)<<" = “<<x<<” "
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

using namespace std;

int main()
{
int t,n,t1,t2,data,ans,L,R;
cin>>t;
while(t–)
{
cin>>n;
int A[n][2];
ans=0;

string s1[n];
for(int i=0;i<n;i++) cin>>s1[i];
  L=R=0; //L,R contains total no. of 1's in left and right respectively

for(int i=0;i<n;i++){
  t1=0;t2=0;
  
for(int j=0;j<n;j++){
  data=(s1[i][j]=='0')?0:1;
  if(j>=0 && j<n/2)
    t1+=data;
  else
    t2+=data;
}
A[i][0]=t1;
A[i][1]=t2;
L+=t1;
R+=t2;

}
// watch(L);
// watch®;

int temp1,temp2;
temp1=L-R;   
ans=abs(temp1);

for(int i=0;i<n;i++)
{
  temp2=abs(temp1-2*(A[i][0]-A[i][1]));
  if(ans>temp2) pur=temp2;
  
}

cout<<ans<<endl;
}

}

why do i get SIGSEGV in my code ?

#include <iostream>
#include<algorithm>
#include<numeric>
#include<vector>
#include<map>
#include<string>
#include<string.h>
#include<math.h>
#define REP(i, a, b)  for (int i = int(a); i <= int(b); i++)
#define NREP(i, a, b) for (int i = int(a); i >= int(b); i--)
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL)
#define circ_inc(i, n) (i + 1) % int(n)
#define circ_dec(i, n) (i + int(n) - 1) % int(n)
#define round(x) (int)((double)int(x) + 0.5)
#define println(x) cout << x << '\n'
#define F first
#define S second
#define INF 1000000000
#define gcd __gcd
#define nset __builtin_popcount
#define pb push_back
#define btoe(x) x.begin(), x.end()
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vector<int> > vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef map<string, int> msi;
typedef istringstream iss;
int main()
{
	FASTIO;
	int t;
	cin >> t;//testcases
	while (t--) {
		int n;
		cin >> n;
		string s[1000];
		REP(i, 0, n - 1) {
			cin >> s[i];

		}
		int leftsum = 0, rightsum = 0;
		int x = 0, y = 0;
		vi v1;//storing total number of 1s on left for every row
		vi v2;//storing total number of 1s on right for every row
		REP(i, 0, n - 1) {
			x = 0;

			REP(j, 0, n / 2 - 1) {
				if (s[i][j] == '1') {
					leftsum++;
					x++;
				}

			}
			v1.pb(x);

		}
		REP(i, 0, n - 1) {
			y = 0;

			REP(j, n / 2, n - 1) {
				if (s[i][j] == '1') {
					rightsum++;
					y++;
				}

			}
			v2.pb(y);

		}

		if (leftsum - rightsum != 0) {

			vi results;
			REP(i, 0, n - 1) {
				cout << (leftsum - rightsum) - 2 * (v1[i] + v2[i]) << endl;
				if ((leftsum - rightsum) - 2 * (v1[i] + v2[i]) == 0)
					continue;
				results.pb((leftsum - rightsum) - 2 * (v1[i] + v2[i]));
			}
			sort(results.begin(), results.end());
			cout << results[0] << endl;
		}
		else
			cout << "0" << endl;
	}

	return 0;
}

I know all about prefix and postfix operators. Changing prefix operators to postfix operators doesn’t effect my answer.

could anybody tell what’s wrong with my ans, please?

https://www.codechef.com/viewsolution/29249720