COVID19 - Editorial

I am not able to debug what the issue is with this code. Any help?

  public static void main(String[] args) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    int testcases = Integer.parseInt(bufferedReader.readLine());
    while(testcases > 0){
      int N = Integer.parseInt(bufferedReader.readLine());
      int[] Xs = Arrays.stream(bufferedReader.readLine().split(" "))
                       .mapToInt(Integer::parseInt)
                       .toArray();
      int prevX = Xs[0];
      int clusterSize = 0;
      int min = 1000;
      int max = -1;
      for(int x : Xs){
        if(x - prevX <= 2){
          clusterSize += 1;
        }
        else {
          if(clusterSize < min){
            min = clusterSize;
          }
          else if(clusterSize > max){
            max = clusterSize;
          }
          clusterSize = 1;
        }
        prevX = x;
      }
      if(min == 1000){
        min = clusterSize;
      }
      if(max == -1){
        max = clusterSize;
      }
      System.out.println(min + " " + max);
      testcases--;
    }
  }

I have been working on this for 4 hours , i have no idea why it is not working…it seems correct to me.
please guys any help i will be very great-full.
https://www.codechef.com/viewsolution/33106375

one thing I can spot is

       if(best=1)

besides this there is also a logical error .
Just check for some testcases.

5
5
0 3 6 9 10 Output : 1 2
7
0 1 4 5 8 9 10 Output: 2 3
6
0 1 4 5 8 9 Output : 2 2
5
0 1 4 7 10 Output : 1 1
8
0 1 2 3 4 5 6 7 Output : 8 8

Grateful …
You are not checking for middle cases where minimum group can be between Some max groups . Besides these min can never be 10 as there are only 8 people .
Just check for these .

4
5
0 3 6 9 10 Output : 1 2
7
0 1 4 5 8 9 10 Output: 2 3
6
0 1 4 5 8 9 Output : 2 2
8
0 1 2 3 4 5 6 7 Output : 8 8

2 Likes

I missed this case, thanks.

1 Like

PLease have alook at my solution in java
https://www.codechef.com/viewsolution/33152156
I think this question is the most easy to understand.

:slightly_smiling_face:

feeling happy to help someone in a way

1 Like

Good Logic

can you find out where i am wrong

how this is possible bro .
5
0 1 4 7 10
output min=1,max=1 max value should be 2 here like in case if 0 is infected or 1 is infected

1 Like

Thanks for correcting me bro.

The link is not correct.

i solved that problem …but when i run my code on code i get runtimeException but when i submit it was correct and successfully submited

1 Like

It aint much but Its honest work

Hi, logan_Pradeep
The way you have given the soultion is AWESOME and clever as well.
Can you please tell me how you approached this solution, I know your experience was there, but apart from that, how you think a newbie can also approach this, like any previous problem which helped you with this approach. Please elaborate.

1 Like

@soumya_8596 always keep pen and paper with you and write keypoints of question on paper and then use the given testcases of problem and try to recognize pattern where this problem is going to converge and we all know practice is most important.

1 Like

I am beginner plz can someone help me print the minimum value in each test case.I am able to print the maximum.

Thank You Pradeep, that was nice. Just I want to add, I do use pen and paper first,I also try to find as many edgecase as possible, then I put them into some condition, to get the answer. But that soution I get is not enough for submission. But your code is handling all edge cases with just some little conditions…If you look at my code here, you can tell where do I lag, what is wrong in my way. Please don’t stretch for my code, or get worry. Thank you for your time and words.

Bro… its not gud to add more comments here. U can connect me on linkedln https://www.linkedin.com/in/pradeepshakya

what’s wrong with my soln;

#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t–){
int n, mn=INT_MAX, mx=1, temp=1;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n-1;i++){
temp=1;
while(abs(a[i]-a[i+1])<=2 && i<n-1){
temp++;
i++;
}
mx=max(temp,mx);
mn=min(temp,mn);
}
cout<<mn<<" "<<mx<<endl;
}
}