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

22 Apr, 2021
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:
C++14
Java
Python3
C#
Javascript
// C++ program to implement// the above approach#include <iostream>using namespace std; // Function to check if the array is// spirally sorted or notbool isSpiralSorted(int arr[], int n){     // Stores start index    // of the array    int start = 0;     // Stores end index    // of an array    int end = n - 1;     while (start < end) {         // If arr[start]        // greater than arr[end]        if (arr[start] > arr[end]) {            return false;        }         // Update start        start++;         // If arr[end]        // greater than arr[start]        if (arr[end] > arr[start]) {            return false;        }         // Update end        end--;    }     return true;} // Driver Codeint main(){    int arr[] = { 1, 10, 14, 20, 18, 12, 5 };     int N = sizeof(arr) / sizeof(arr[0]);     // Function Call    if (isSpiralSorted(arr, N))        cout << "YES" << endl;    else        cout << "NO" << endl;    return 0;}
// Java program to implement// the above approachimport java.util.*; class GFG{   // Function to check if the array is  // spirally sorted or not  static boolean isSpiralSorted(int[] arr, int n)  {     // Stores start index    // of the array    int start = 0;     // Stores end index    // of an array    int end = n - 1;     while (start < end)    {       // If arr[start]      // greater than arr[end]      if (arr[start] > arr[end])      {        return false;      }       // Update start      start++;       // If arr[end]      // greater than arr[start]      if (arr[end] > arr[start])      {        return false;      }       // Update end      end--;    }           return true;  }   // Driver code  public static void main(String[] args)  {    int[] arr = { 1, 10, 14, 20, 18, 12, 5 };      int N = arr.length;     // Function Call    if (isSpiralSorted(arr, N) != false)      System.out.print("YES");    else      System.out.print("NO");  }} // This code is contributed by sanjoy_62
# Python3 program to implement# the above approach # Function to check if the array is# spirally sorted or notdef isSpiralSorted(arr, n) :     # Stores start index    # of the array    start = 0;     # Stores end index    # of an array    end = n - 1;    while (start < end) :         # If arr[start]        # greater than arr[end]        if (arr[start] > arr[end]) :            return False;         # Update start        start += 1;         # If arr[end]        # greater than arr[start]        if (arr[end] > arr[start]) :            return False;         # Update end        end -= 1;    return True; # Driver Codeif __name__ ==  "__main__" :    arr = [ 1, 10, 14, 20, 18, 12, 5 ];    N = len(arr);     # Function Call    if (isSpiralSorted(arr, N)) :        print("YES");    else :        print("NO");             # This code is contributed by AnkThon
// C# program to implement// the above approachusing System;using System.Collections.Generic;class GFG{         // Function to check if the array is    // spirally sorted or not    static bool isSpiralSorted(int[] arr, int n)    {               // Stores start index        // of the array        int start = 0;               // Stores end index        // of an array        int end = n - 1;               while (start < end)        {                   // If arr[start]            // greater than arr[end]            if (arr[start] > arr[end])            {                return false;            }                   // Update start            start++;                   // If arr[end]            // greater than arr[start]            if (arr[end] > arr[start])            {                return false;            }                   // Update end            end--;        }               return true;    }   // Driver code  static void Main()  {    int[] arr = { 1, 10, 14, 20, 18, 12, 5 };      int N = arr.Length;       // Function Call    if (isSpiralSorted(arr, N))        Console.WriteLine("YES");    else        Console.WriteLine("NO");  }} // This code is contributed bydivyesh072019
<script>    // Javascript program to implement    // the above approach         // Function to check if the array is    // spirally sorted or not    function isSpiralSorted(arr, n)    {                // Stores start index        // of the array        let start = 0;                // Stores end index        // of an array        let end = n - 1;                while (start < end)        {                    // If arr[start]            // greater than arr[end]            if (arr[start] > arr[end])            {                return false;            }                    // Update start            start++;                    // If arr[end]            // greater than arr[start]            if (arr[end] > arr[start])            {                return false;            }                    // Update end            end--;        }                return true;    }         let arr = [ 1, 10, 14, 20, 18, 12, 5 ];     let N = arr.length;        // Function Call    if (isSpiralSorted(arr, N))        document.write("YES");    else        document.write("NO");     </script>
YES
 
Time Complexity: O(N) Auxiliary Space: O(1)
divyesh072019
sanjoy_62
ankthon
suresh07
array-traversal-question
Arrays
Greedy
Mathematical
School Programming
Searching
Arrays
Searching
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Dijkstra's shortest path algorithm | Greedy Algo-7
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Huffman Coding | Greedy Algo-3
Write a program to print all permutations of a given string