VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-if-a-given-array-is-sorted-in-spiral-manner-or-not/

⇱ Check if a given array is sorted in Spiral manner or not - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if a given array is sorted in Spiral manner or not

Last Updated : 23 Jul, 2025

Given an array arr[] of size N, the task is to check if the array is spirally sorted or not. If found to be true, then print "YES". Otherwise, print "NO".

Note: An array is spirally sorted if arr[0] ? arr[N - 1] ? arr[1] ? arr[N - 2] ...

Examples:

Input: arr[] = { 1, 10, 14, 20, 18, 12, 5 } 
Output: YES 
Explanation: 
arr[0] < arr[6]
arr[1] < arr[5]
arr[2] < arr[4]
Therefore, the required output is YES.

Input: arr[] = { 1, 2, 4, 3 } 
Output: NO

Approach: The idea is to traverse the array and for every array element, say arr[i], check if arr[i] is less than or equal to arr[N - i - 1] and arr[N - i - 1] less than or equal to arr[i + 1] or not. If found to be false, then print "NO". Otherwise, if all array elements satisfy the condition, then print "YES". Follow the steps below to solve the problem:

  • Initialize two variables, say start and end, to store the start and end indices of the given array.
  • Iterate a loop while start is less than end, and check if arr[start] less than or equal to arr[end] and arr[end] is less than or equal to arr[start + 1] or not. If found to be false, then print "NO".
  • Otherwise, print "YES".

Below is the implementation of the above approach:


Output: 
YES

 

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

Comment
Article Tags: