Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Check if array is sorted

Easy
0/40
profile
Contributed by
0 upvote

Problem statement

You are given an array ‘A’ of length ‘N’ , determine whether the array is sorted in non-decreasing order or not.


Example:
Input:
A = [1, 3, 6, 10]
Output:
True
Explanation: Since array ‘A’ is sorted, therefore output is ‘True’.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of the input contains an integer ‘N’.
The next line will contain ‘N’ space-separated integers.
Output Format:-
The output contains a string 'True' or 'False'.
Note:-
You don’t need to print anything. Just implement the given function.
Sample Input 1:
5
10 20 30 20 40
Sample Output 1:
False
Explanation Of Sample Input 1:
A = [10, 20, 30, 20, 40]
Since array ‘A’ is not sorted, therefore output is ‘False’.
Sample Input 2:
1
1100
Sample Output 2:
True
Constraints:
1 <= N  <= 10^5
1 <= A[i] <= 10^9
Hint

In a non-decreasing array, adjacent elements are increasing.

Approaches (1)
Iterative Method

Approach:

 

  • Iterate over the array and check if ‘A[i-1]’ <= ‘A[i]’, for all ‘i’ from 1 to ‘N’-1. (0-based indexing)
  • If the above condition is satisfied, then return true.
  • Else, return false.

 

Algorithm:

 

  1. For ‘i’ from 1 to ‘N’-1:
    • If ‘A[i-1]’ > ‘A[i]’:
      • Return false
  2. Return true.
Time Complexity

O( N ), Where ‘N’ is the array ‘A’ length.

 

Iterating over an array of length ‘N’ takes O(N) complexity. Hence, the overall time complexity will be O( N ).

Space Complexity

O( 1 ).

 

We are taking O( 1 ) extra space for variables. Hence, the overall space complexity will be O( 1 ).

Code Solution
(100% EXP penalty)
Check if array is sorted
All tags
Sort by
Search icon

Interview problems

Very simple in C++ (Check arraySortedOrNot)

bool arraySortedOrNot(vector<int>&A, int N) {

   if(is_sorted(A.begin(),A.end())==true) return true;

 

   return false;

}

 

2 views
0 replies
0 upvotes

Interview problems

beginner friendly java solution

public class Solution {

public static boolean arraySortedOrNot(int []A, int N) {

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

   if(A[i-1]>A[i])

       return false;

return true;

}

}

3 views
0 replies
0 upvotes

Interview problems

Java | 2 Liner | O(n)

Please upvote if its helpful !

public class Solution {
    public static boolean arraySortedOrNot(int []A, int N) {
        for(int i = 1; i<N;i++) if(A[i-1]>A[i]) return false;
        return true;
    }
}

java

50 views
0 replies
0 upvotes

Interview problems

python

n=len(a)

    if(n==1):

        return True

    for i in range(1,n):

        if(a[i]<a[i-1]):

            return False

    return True

    pass

 

59 views
0 replies
0 upvotes

Interview problems

Runtime errror

C++

 

bool arraySortedOrNot(vector<int>&A, int N) {

     if (N <= 1)   return true; 

        if (A[N - 2] > A[N - 1]) return false;            

        return arraySortedOrNot(A, N - 1);  

}

 

 

 

JAVA

 

public class Solution {

 public static boolean arraySortedOrNot(int []A, int N) {

        if (N <= 1)   return true; 

        if (A[N - 2] > A[N - 1]) return false;           

        return arraySortedOrNot(A, N - 1);          

    }

}

 

CPP code is working fine but the same code in java is giving runtime error at 11/12th test case. what is the issue ?  

92 views
0 replies
0 upvotes
Full screen
Console