CHEFZOT - Editorial

#include
#define max 100000
using namespace std;

int main()
{

unsigned long long int N, a[max], i, j = 1;
unsigned long long int maxim = 0, maxfin = 0;

cin >> N;
for(i = 0; i < N; i++)
	cin >> a[i];

if(N == 1)
{
	if(a[0] == 0)
		maxfin = 0;
	else
		if(a[0] != 0)
			maxfin = 1;
}
else
{
for(i = 0; i < N && j <= N;)
{
	
	if(a[i] != 0)
		maxim++;

	while(a[i]*a[j] != 0)
	{
		j++;
		maxim++;
	}
	
	i = j + 1;
	j = i + 1;
	
	if(maxim > maxfin)
		maxfin = maxim;

	maxim = 0;
}

}

cout << maxfin;

}

@kellymilla18

this approach too gives a wrong answer…i dont think that was the prob indeed

Well this solution can be optimized more in terms of space requirement by not taking the array. Instead take an element one by one and increment the count. If the input turns out to be zero then set count equal to zero
The code can be

int ans=0,c=0,i;
for(i=0;i<n;i++){
scanf("%d",&x)
if(x>0)
c++;
else
c=0;
if(c>ans)
 ans=c;
}
5 Likes

Hey all ! Can anyone tell whats wrong with this code.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

class ChefZot {
int size=0,N=0,count1=0, count2=0;
int arr[]=new int[10000];

 public void solve() throws Exception{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    PrintWriter out=new PrintWriter(System.out);
    StringTokenizer st;
    N=Integer.parseInt(br.readLine());
    st=new StringTokenizer(br.readLine());
           for(int i=0;i<N;i++)
               arr[i]=Integer.parseInt(st.nextToken());
           size=N;            

           for(int j=0;j<N;j++)
              {
             if(arr[j]!=0)
              count1++;
             else if(arr[j]==0)
                {
                 if(count1>=size/2)
                   { 
                    System.out.println(count1);
                    System.exit(0);
                   }
                 else if(count1<size/2)
                   {
                   if(count1>count2)
                   count2=count1;
                    count1=0;
                   continue;
                   }
        }
    
      if(j==N-1)
      {
          if(count1>count2)
          count2=count1;
      }           
   }
         System.out.println(count2);
         br.close();
    }
 public static void main(String[] args)
     {
          ChefZot c=new ChefZot();
           try{ 
             c.solve();
              }
            catch(Exception e){
               System.exit(0);
                   }
    
     }

}

please tell me what’s wrong with this code?? It’s showing correct outputs but still there’s a ‘Wrong Answer’ to my solution…

#include
using namespace std;

int main()
{
int n,i,*a,*b,maxi,ct=0;
cin>>n;
a=new int[n];
b=new int[n];
for(i=0;i<n;i++)
{
cin>>a[i];
if(a[i]==0)
{
b[ct]=i;
ct++;
}
}
maxi=b[0];
for(i=1;i<ct;i++)
{
if(maxi<(b[i]-b[i-1]-1))
maxi=b[i]-b[i-1]-1;
}
if(maxi<n-b[ct-1]-1)
maxi=n-b[ct-1]-1;

cout<<maxi;
return 0;
}

how/where to get java solution for June2014 problems???

1 Like

int main()
{
long int n,maxLength=1,currentLength=0,x=0;
cin>>n;
long int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]==0)
x++;
}

if(x==n)
    cout<<"0\n";
else
{
    for(int i=0;i<n;i++)
    {
        if(a[i]>0)
        {
            currentLength++;
        }

        else
        {
            currentLength=0;
        }

        if(currentLength>maxLength)
        {
            maxLength=currentLength;
        }
    }
    cout<<maxLength<<"\n";
}

return 0;

}

def fun(a,n):
    l=0
    ans=0
    for i in range(1,n):
        if(a[i]==0):
            l=i
        cur=i-l
        ans=max(ans,cur)
    print(ans)
n=int(input())
a=list(map(int,input().split()))
fun(a,n)

This is always giving wrong answer ,how it can be improved ?

You can use this link :

or just click (practise) of the PROBLEM LINK at the start of this question

your solution is wrong for some inputs where N = 1… since j is always starting at 1…
it will not enter the for loop and the maxfin will remain equals to 0… the answer for all N = 1 is 1 if a[0] != 0…

1 Like

oh damn…i neva thought about this and always thought my code was unable to handle large data…thanx a ton :slight_smile:

i think u have to use #define in place of define

@sanzzzay

I couldn’t post the code with ‘#’ here…rest assured there arent any compilation errors…kellymilla18 solved the prob

thanx anyway

1 Like

ohh sorry i was just trying to be smarter i guess

hehe…anyway…the prob still persists n i m not able to find logical errors :stuck_out_tongue:

try this… N = 5, 1 0 0 1 1… output should be 2… CodeChef: Practical coding for everyone

Please post the link of your submitted code.

Better than editorial :grin:

For the following solution I get AC :

n = int(input())
    
li = list(map(int,input().split()))
count = 0
maximum = 0
i = 0
while i < len(li):
        
    if li[i]>0:
        count = count+1
        if maximum <= count:
            maximum = count
    else:
        count = 0
        
    i+=1
            
print(maximum)

For the following I get WA:

n = int(input())
    
li = list(map(int,input().split()))
count = 0
maximum = 0
i = 0
while i < len(li):
        
    if li[i]>0:
        count = count+1
    else:
        if maximum <= count:
            maximum = count
        count = 0
        
    i+=1
            
print(maximum)

Any Idea on what test case might the one with WA fail? Seems like the same code to me.

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

Here is a link to my solution in Python 3.6. I follow a different way to get to the solution but one of the test case seems to be failing. Can anyone help me find out what it might be

hey @shivamgutgutia,
Congrats on posting your first query!
Here is a test case in which give a wrong answer :point_down:

3
10 10 10

the correct answer is 3 but your code gives 1.

you are taking nums input as

nums=’ '.join(input().strip().split())

Remember that input() will cast the input as a string. Thus for above mention input, your nums will be “10 10 10”.
If you split this string with ‘0’ nums become [“1”, " 1", " 1"] .
Thanks.

1 Like