Little elephant and permutation giving wrong answer.please help

http://www.codechef.com/problems/LEPERMUTlink text

   #include<iostream>
   #include<algorithm>
    #define scanint(a) scanf("%d",&a)
    using namespace std;
     int main()
    {
int test;
scanint(test);
while(test--)
{
  int n,local_inverse=0,inverse=0;
  scanint(n);
  int arr[n];
  for(int i=1;i<=n;i++)
  scanint(arr[i]);
  for(int i=1;i<=n;i++)
  {
          for(int j=i+1;j<=n;j++)
          {
                             if(arr[i]>arr[j])
                             inverse++;
                             }
                             }
                             for(int i=1;i<=n;i++)
                             {
                                   if(arr[i]>arr[i+1])
                                   local_inverse++;
                                   }
                                   if(inverse==local_inverse)
                                   printf("YES\n");
                                   else
                                   printf("NO\n");
                                   }
                 return 0;
                                   }  

There is a small correction in this loop

for(int i=1;i<=n;i++)

{

if(arr[i]>arr[i+1])

local_inverse++;

}

In this loop for when i=n arr[n+1] will give garbage value.

So, change the condition to i < n.

This should do it.

If still there is any problem you can comment below.

1 Like

Thanks a lot

thank u so much…it worked :slight_smile:

If the question is answered correctly, accept the answer :slight_smile: