Check if array is sorted | Practice | GeeksforGeeks

Given an array arr[] of size N, check if it is sorted in non-decreasing order or not. 
Example 1:
Input:
N = 5
arr[] = {10, 20, 30, 40, 50}
Output: 1
Explanation: The given array is sorted.

Example 2:
Input:
N = 6
arr[] = {90, 80, 100, 70, 40, 30}
Output: 0
Explanation: The given array is not sorted.

Your Task:
You don't need to read input or print anything. Your task is to complete the function arraySortedOrNot() which takes the arr[] and N as input parameters and returns a boolean value (true if it is sorted otherwise false).

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 105
1 ≤ Arr[i] ≤ 106
 
0
dell94593 weeks ago
java:class Solution {   boolean arraySortedOrNot(int[] arr, int n) {       // code here       for (int i =0; i<arr.length-1;i++){           if(arr[i]>arr[i+1]){               return false;           }       }       return true;   }}
+1
princejee20191 month ago
C++ CODE || EASY TO UNDERSTAND
bool arraySortedOrNot(int arr[], int n) {   for(int i =0;i<n-1;i++){       if(arr[i]>arr[i+1]){           return false;       }   }   return true;}
0
codewithaddy2 months ago
SIMPLE C+++ SOLUTION
bool arraySortedOrNot(int a[], int n) {

   for(int i=0; i<n-1; i++){
       if(a[i]>a[i+1]){
           return false;
       }
   }
    return true;
}
0
deepanshubhalla20002 months ago
class Solution {   boolean arraySortedOrNot(int[] arr, int n) {       return helper(arr,n,0);       // code here   }   static boolean helper(int[] arr, int n, int index){       if(index==n-1) return true;               return arr[index] <= arr[index+1] && helper(arr,n,index+1);   }}
0
godvishalkumar7862 months ago
        if(arr.length==1){           return true;       }           boolean flag=false;  for(int i=0; i<n-1; i++){      if(arr[i]<=arr[i+1]){          flag=true;      }      else{          flag=false;          break;   }}return flag;
0
mohdabidalistates2 months ago
Hi I have a doubt. Can anyone tell why my code doesn't work.
 
 
    bool flag=false;   for(int i=0; i<n; i++){       if(arr[i]<=arr[i+1]){           flag=true;       }       else{           flag=false;           break;       }   }      return flag;
0
shulabhgill2 months ago
class Solution{   public:   bool arraySortedOrNot(int arr[], int n) {       bool res=true;      if(n==0 || n==1)      return true;            if(arr[0]>arr[1])      return false;      else      {           res=arraySortedOrNot(arr+1,n-1);      }            return res;               }};
0
shivamrajpoot24102 months ago
  for(int i=1;i<n;i++){           if(arr[i]<arr[i-1]){               return false;           }                                      }        return true;   }
+1
shivamray3 months ago
 bool arraySortedOrNot(int arr[], int n) {       for (int i=0;i<n-1;i++)// code here   {       if (arr[i]>arr[i+1])       {           return false;       }          }       return true;   } 
+1
niks043 months ago
bool arraySortedOrNot(int arr[], int n) {
    for(int i=1;i<n;i++){
        if(arr[i]<arr[i-1])
           return false;
    }
    return true;
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.