# Program to check if an array is sorted or not

QUESTION

Given an array of size n, write a program to check if it is sorted in ascending order or not. Equal values are allowed in array and two consecutive equal values are considered sorted.

“TESTCASE_1”: “5\n6 8 9 2 1\n###—###SEPERATOR—###—\nNo”, “TESTCASE_2”: “6\n1 2 3 4 5 6\n###—###SEPERATOR—###—\nYes”, “TESTCASE_3”: “7\n1 23 56 4 8 45 32\n###—###SEPERATOR—###—\nNo”, “TESTCASE_4”: “4\n0 -1 -3 8\n###—###SEPERATOR—###—\nNo”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

``````import java.io.*;
import java.util.*;
class TestClass
{
// Function that returns 0 if a pair
// is found unsorted
static int arraySortedOrNot(int arr[], int n)
{
// Array has one or no element or the
// rest are already checked and approved.
if (n == 1 || n == 0)
return 1;

// Unsorted pair found (Equal values allowed)
if (arr[n-1] < arr[n-2])
return 0;

// Last pair was sorted
// Keep on checking
return arraySortedOrNot(arr, n-1);
}

// main function
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
if (arraySortedOrNot(arr, n)!=0)
System.out.println("Yes");
else
System.out.println("No");
}
}``````

#### Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.