Check if an array is sorted

PROBLEM :

Given an array, write a program that prints 1 if array is sorted in non-decreasing order, else prints 0.

Input:

The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,N is the size of array.
The second line of each test case contains N input C[i].

Output:

Print 1 if array is sorted, else print 0.

Constraints:

1 = T = 100
1 = N = 500
1 = C[i] = 1200

Example:

Input
2
5
10 20 30 40 50
6
90 80 100 70 40 30

Output
1
0

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
int main()
 {
int no,i,ele,j,a[1200],flag=0 ;
cin>>no ;

while(no--)
{
   cin>>ele ;
   for(j=0;j<ele;j++)
       cin>>a[j] ;
     
   flag=0 ;
   
  for(j=0;j<ele-1;j++)
  {
      if(a[j]>a[j+1])
           flag=1 ;
  }
 
  if(flag==1)
cout<<0 ;
  else
    cout<<1 ;
 
   cout<<endl ;
}
return 0;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )