pkpawan's blog

By pkpawan, history, 5 years ago, In English

Here is the question :-https://codeforces.me/contest/1270/problem/B

what i understood from the question that we need to find subarrays such that the difference of maximum and minimun element is equal to N (total emenets of array ) ,if it is not possible print "NO" else print "YES" and in next line print 1 and 'ending index' but if we are given an array 'A' and it is statifying condition max(A)-max(B)>=N then according to question answer should be 'YES' 1 N(size of array)

which we even need to check for subarray ?that's all I understood from question please help me out in understanding correctly

here's my code:- ```

include<bits/stdc++.h>

using namespace std;

int main() { int t; cin>>t; while(t--) { int n,f=0; cin>>n;

int A[n],i,j,min=0,max=0,l,r;
   for(i=0;i<n;++i)
   {
       cin>>A[i];
       if(max<A[i])
       {
           max=A[i];         //finding max
       }

   }
   min=A[0];
   for(i=1;i<n;++i)
   {
       if(min>A[i])
       { 
           min=A[i];               //finding min
       } 
   }


  if(max-min>=n)                   //checking condition max(A)-min(A)=n
  {
      for(i=0;i<n;++i)
      {
          if(A[i]==min)            //checking at what index is min and max present
          {
              l=i+1;                  //adding 1 in index as index taken from 0 to n-1
          }
          if(A[i]==max)
          {
              r=i+1;
          }
      }
      if(l<r)                           //checking which is greater and printing accordingly
      {
      cout<<"YES"<<endl;
      cout<<l<<" "<<r<<endl;
      }
      else
      {
          cout<<"YES"<<endl;
      cout<<r<<" "<<l<<endl;
      }

  }
  else
  {
      cout<<"NO"<<endl;
  }

}

return 0;

}```

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Try the case: 8

1 2 4 0 0 0 0 5

»
5 years ago, # |
Rev. 6   Vote: I like it 0 Vote: I do not like it

This was my idea as well. It fails on the following test:

1

3

1 3 2

The correct solution checks for every two adjacent numbers if differ by at least 2. If at least two differ by at least 2 (For the test above 1 and 3) print the corresponding positions in the array(For the test above 1 and 2).

Your idea would have printed "NO" instead of "YES endl 1 2".

Memory: O(n) or O(1), not necessary

Complexity: O(n)