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

Check Sorted Array

Easy
0/40
Average time to solve is 12m
profile
Contributed by
275 upvotes
Asked in company
Tata Consultancy Services (TCS)

Problem statement

You have been given an array ‘a’ of ‘n’ non-negative integers.You have to check whether the given array is sorted in the non-decreasing order or not.


Your task is to return 1 if the given array is sorted. Else, return 0.


Example :
Input: ‘n’ = 5, ‘a’ = [1, 2, 3, 4, 5]
Output: 1

The given array is sorted in non-decreasing order; hence the answer will be 1.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line will contain the integer ‘n’, the number of elements in the array ‘a’, and the next line will contain the ‘n’ spaced integers in the array elements. 
Output Format:
Print 1 if the given array is sorted. Else, print 0.

Output for each test case will be printed on a separate line.
Note:
You are not required to print anything; it has already been taken care of. Just implement the function.
Sample Input 1 :
4
0 0 0 1
Sample Output 1 :
1
Explanation For Sample Input 1 :
The given array is sorted in non-decreasing order; hence the answer will be 1. 
Sample Input 2 :
5
4 5 4 4 4
Sample Output 2 :
0
Expected Time Complexity:
O(n), Where ‘n’ is the size of an input array ‘a’.
Constraints:
1 ≤ 'n' ≤ 5*10^6
0 ≤ 'a'[i] ≤ 10^9

Time limit: 1 sec
Hint

Can you think of comparing the adjacent elements here?

Approaches (1)
Naive Approach

Approach

 

We will simply iterate over the array and if for every element (except the end) the element after is less than the current element then we will return 0 immediately. After exiting from the loop we will return 1.
 

Algorithm: 
 

  • Iterate from 0 to ‘n’ over an array ‘a'
    • If ‘a[i + 1] < a[i]’ then return 0
  • Return 1
Time Complexity

O(n), Where ‘n’ is the size of the array ‘a’.
 

We are simply iterating over the array ‘a’.

 

Hence, the time complexity is O(n).

Space Complexity

O(1).
 

We are using constant extra space.

 

Hence, the space complexity is O(1).

Video Solution
Unlock at level 3
(75% EXP penalty)
Code Solution
(100% EXP penalty)
Check Sorted Array
All tags
Sort by
Search icon

Interview problems

Simple C++ Solution

int isSorted(int n, vector<int> arr) {
    // Write your code here.
    for(int i=1;i<n;i++)
    {
      if(arr[i]>=arr[i-1])
      {
      }
      else
      {
        return 0;
      }
     
    }
    return 1;
}
11 views
0 replies
0 upvotes

Interview problems

solution for c++ bruteforce approch

int isSorted(int n, vector<int> a) {

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

 

    {

 

        if(a[i+1]<a[i])

 

        {

 

            return 0;

 

        }

 

    }

 

    return 1;

 

}

 

47 views
0 replies
1 upvote

Interview problems

JAVA || Easy Code || O(n)

public class Solution {

    public static int isSorted(int n, int []a) {

        // Write your code here.

        

        for(int i=0;i<n-1;i++){

            if(a[i]>a[i+1]){

                return 0;

            }

        }

        return 1;

    }

}

java

44 views
0 replies
0 upvotes

Interview problems

JAVA

public class Solution {
    public static int checkSorted(int currentIndex, int[] arr, int n){
        if(currentIndex == 0){
            return checkSorted(currentIndex+1, arr, n);
        }
        if(arr[currentIndex] < arr[currentIndex - 1]){
            return 0;
        }
        if(currentIndex == n){
            return 1;
        }
        return checkSorted(currentIndex+1, arr, n);
    }
    public static int isSorted(int n, int []a) {
        // Write your code here.
        return checkSorted(0, a, n-1);
    }
}

java

programming

76 views
0 replies
0 upvotes

Interview problems

Java Solution

public class Solution {

    public static int isSorted(int n, int []a) {

       

     int max = a[n-1];

       for(int i=n-2;i>=0;i--){

 

        

            if(a[i]>max){

               return 0;

           }

       }

 

       return 1;

    }

}

101 views
0 replies
0 upvotes

Interview problems

SORTED ARRAY OR NOT (JAVA OPTIMAL SOLLUTION)

public class Solution {

    public static int isSorted(int n, int []a) {

        // Write your code here.

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

            if (a[i] >= a[i-1]) {

                

            } else {

                return 0;

            }

        }

        return 1;

    }

}

72 views
0 replies
0 upvotes

Interview problems

JAVA SOLUTION

public class Solution {

    public static int isSorted(int n, int []a) {

        // Write your code here.

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

            if(a[i] >= a[i-1]){

                

            }else{

                return 0;

            }

        }

        return 1;

    }

}

java

51 views
0 replies
0 upvotes

Interview problems

Check the array is sorted or not

//Asked in tcs company

 

//HOW TO CHECK WHETHER UR ARRAY IS SORTED?

int isSorted(int n, vector<int> a) {

    // Write your code here.

  

    for (int i = 0; i < n-1; i++) {

      if (a[i] >a[i+1]) {

        return 0;

      }

    }

    return 1;

}

//eg: 1,2,8,15

//1>2

//2>8

//8>15

 

59 views
0 replies
0 upvotes

c++ approch

int isSorted(int n, vector<int> a) {

    // Write your code here.

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

    {

        if(a[i+1]<a[i])

        {

            return 0;

        }

    }

    return 1;

}

//Follow me on Linkedin :www.linkedin.com/in/gschandrasekhar

115 views
0 replies
0 upvotes

Interview problems

C++ Solution

int isSorted(int n, vector<int> a) {

    // Write your code here.

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

        if(a[i-1]<=a[i] && a[i+1]>=a[i]){

            return 1;

        }

        else{

            return 0;

        }

    }

}

 

82 views
0 replies
0 upvotes
Full screen
Console