Check if a given array is sorted in Spiral manner or not
Last Updated :
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
// C++ program to implement
// the above approach
#include <iostream>
using namespace std;
// Function to check if the array is
// spirally sorted or not
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
int 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
// Java program to implement
// the above approach
import 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
# Python3 program to implement
# the above approach
# Function to check if the array is
# spirally sorted or not
def 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 Code
if __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#
// C# program to implement
// the above approach
using 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
JavaScript
<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>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Check if a given array is pairwise sorted or not An array is considered pairwise sorted if each successive pair of numbers is in sorted (non-decreasing) order. In case of odd elements, last element is ignored and result is based on remaining even number of elements. Examples: Input : arr[] = {10, 15, 9, 9, 1, 5}; Output : Yes Pairs are (10, 15), (
5 min read
Check whether a given array is a k sorted array or not Given an array of n distinct elements. Check whether the given array is a k sorted array or not. A k sorted array is an array where each element is at most k distances away from its target position in the sorted array. For example, let us consider k is 2, an element at index 7 in the sorted array, c
12 min read
Check if Array can be sorted in non-decreasing order using given operations Given an array arr[] of size N consisting of positive integers, the task is to check if the array can be sorted in non-decreasing order by performing the following operations: Select two adjacent elements.Swap the elements and invert their signs.All elements in the sorted array received at the end,
8 min read
Check if an array is sorted and rotated Given an array arr[] of size n, the task is to return true if it was originally sorted in non-decreasing order and then rotated (including zero rotations). Otherwise, return false. The array may contain duplicates.Examples:Input: arr[] = { 3, 4, 5, 1, 2 }Output: YESExplanation: The above array is so
7 min read
Check if an Array is Sorted Given an array of size n, the task is to check if it is sorted in ascending order or not. Equal values are allowed in an array and two consecutive equal values are considered sorted.Examples: Input: arr[] = [20, 21, 45, 89, 89, 90]Output: YesInput: arr[] = [20, 20, 45, 89, 89, 90]Output: YesInput: a
9 min read
Check if an array is stack sortable Given an array arr[] of n distinct elements, where each element is between 1 and n (inclusive), determine if it is stack-sortable.Note: An array a[] is considered stack-sortable if it can be rearranged into a sorted array b[] using a temporary stack stk with the following operations:Remove the first
6 min read
Sort the given Array in Spiral manner starting from the center Given an array arr[] of size N, the task is to sort the array in descending order starting from the mid of the array having the next largest element at the right of the middle element and the next largest at the left of the middle element and so on. Examples: Input: arr[] = {4, 9, 3, 5, 7}Output: 3
9 min read
Check if given array is almost sorted (elements are at-most one position away) Given an array with n distinct elements. An array is said to be almost sorted (non-decreasing) if any of its elements can occur at a maximum of 1 distance away from their original places in the sorted array. We need to find whether the given array is almost sorted or not.Examples: Input : arr[] = {1
11 min read
Check if the array can be sorted using swaps between given indices only Given an array arr[] of size N consisting of distinct integers from range [0, N - 1] arranged in a random order. Also given a few pairs where each pair denotes the indices where the elements of the array can be swapped. There is no limit on the number of swaps allowed. The task is to find if it is p
15+ min read
Javascript Program for Check if an array is sorted and rotated Given an array of N distinct integers. The task is to write a program to check if this array is sorted and rotated counter-clockwise. A sorted array is not considered as sorted and rotated, i.e., there should at least one rotation.Examples: Input : arr[] = { 3, 4, 5, 1, 2 } Output : YES The above ar
3 min read