Given an integer array **arr** of size **N** which contains distinct elements from **1** to **N**. The task is to check if a position in the array can be found such that all numbers from **1** to** N** can be counted in clockwise direction or counterclockwise direction. **Examples:**

Input:arr[] = [2, 3, 4, 5, 1]Output:YESExplanation:1 2 5 3 4 If counting is start at index 4 then all numbers can be counted from 1 to N in a clockwise order.Input:arr[] = {1, 2, 3, 5, 4]Output:NOExplanation:There is no any index in array from which given array can count 1 to N in clockwise order or counterclockwise order.

**Approach:** The above problem can be solved by observation and analysis.

- An index in the array can be found only if the count of the absolute difference between the consecutive elements greater than
**1**is exactly**1**, because then only it will be possible to count from**1**to**N**in clockwise or counterclockwise order. - If the count of the absolute difference between the adjacent elements is more than
**1**Then it will be impossible to count from**1**to**N**in clockwise or counterclockwise order.

Below is the basic implementation of the above approach:

## C++

`// C++ program to check Clockwise or ` `// counterclockwise order in an array ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `bool` `check_order(vector<` `int` `> arr) ` `{ ` ` ` `int` `cnt = 0; ` ` ` `for` `(` `int` `i = 0; i < arr.size() - 1; i++) { ` ` ` `if` `(` `abs` `(arr[i + 1] - arr[i]) > 1) ` ` ` `cnt++; ` ` ` `} ` ` ` `// Comparing the first and last ` ` ` `// value of array ` ` ` `if` `(` `abs` `(arr[0] - arr[arr.size() - 1]) > 1) ` ` ` `cnt++; ` ` ` ` ` `// If the Count is greater ` ` ` `// than 1 then it can't be ` ` ` `// represented in required order ` ` ` `if` `(cnt > 1) ` ` ` `return` `false` `; ` ` ` `return` `true` `; ` `} ` ` ` `// Driver function ` `int` `main() ` `{ ` ` ` `vector<` `int` `> arr = { 2, 3, 4, 5, 1 }; ` ` ` `if` `(check_order(arr)) ` ` ` `cout << ` `"YES"` `; ` ` ` `else` ` ` `cout << ` `"NO"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to check clockwise or ` `// counterclockwise order in an array ` `class` `GFG{ ` ` ` `static` `boolean` `check_order(` `int` `[]arr) ` `{ ` ` ` `int` `cnt = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < arr.length - ` `1` `; i++) ` ` ` `{ ` ` ` `if` `(Math.abs(arr[i + ` `1` `] - ` ` ` `arr[i]) > ` `1` `) ` ` ` `cnt++; ` ` ` `} ` ` ` ` ` `// Comparing the first and last ` ` ` `// value of array ` ` ` `if` `(Math.abs(arr[` `0` `] - ` ` ` `arr[arr.length - ` `1` `]) > ` `1` `) ` ` ` `cnt++; ` ` ` ` ` `// If the Count is greater ` ` ` `// than 1 then it can't be ` ` ` `// represented in required order ` ` ` `if` `(cnt > ` `1` `) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `[]arr = { ` `2` `, ` `3` `, ` `4` `, ` `5` `, ` `1` `}; ` ` ` ` ` `if` `(check_order(arr)) ` ` ` `System.out.print(` `"YES"` `); ` ` ` `else` ` ` `System.out.print(` `"NO"` `); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to check clockwise or ` `# counterclockwise order in an array ` `def` `check_order(arr): ` ` ` ` ` `cnt ` `=` `0` ` ` `for` `i ` `in` `range` `(` `len` `(arr) ` `-` `1` `): ` ` ` `if` `(` `abs` `(arr[i ` `+` `1` `] ` `-` `arr[i]) > ` `1` `): ` ` ` `cnt ` `+` `=` `1` ` ` ` ` `# Comparing the first and last ` ` ` `# value of array ` ` ` `if` `(` `abs` `(arr[` `0` `] ` `-` `arr[` `len` `(arr) ` `-` `1` `]) > ` `1` `): ` ` ` `cnt ` `+` `=` `1` ` ` ` ` `# If the count is greater ` ` ` `# than 1 then it can't be ` ` ` `# represented in required order ` ` ` `if` `(cnt > ` `1` `): ` ` ` `return` `False` ` ` ` ` `return` `True` ` ` `# Driver code ` `arr ` `=` `[ ` `2` `, ` `3` `, ` `4` `, ` `5` `, ` `1` `] ` ` ` `if` `(check_order(arr)): ` ` ` `print` `(` `"YES"` `) ` `else` `: ` ` ` `print` `(` `"NO"` `) ` ` ` `# This code is contributed by Vishal Maurya. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to check clockwise or ` `// counterclockwise order in an array ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `static` `bool` `check_order(` `int` `[]arr) ` `{ ` ` ` `int` `cnt = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < arr.Length - 1; i++) ` ` ` `{ ` ` ` `if` `(Math.Abs(arr[i + 1] - ` ` ` `arr[i]) > 1) ` ` ` `cnt++; ` ` ` `} ` ` ` ` ` `// Comparing the first and last ` ` ` `// value of array ` ` ` `if` `(Math.Abs(arr[0] - ` ` ` `arr[arr.Length - 1]) > 1) ` ` ` `cnt++; ` ` ` ` ` `// If the Count is greater ` ` ` `// than 1 then it can't be ` ` ` `// represented in required order ` ` ` `if` `(cnt > 1) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]arr = { 2, 3, 4, 5, 1 }; ` ` ` ` ` `if` `(check_order(arr)) ` ` ` `Console.Write(` `"YES"` `); ` ` ` `else` ` ` `Console.Write(` `"NO"` `); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

**Output:**

YES

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Range sum queries for anticlockwise rotations of Array by K indices
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Check if permutaion of one string can break permutation of another
- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
- Move matrix elements in given direction and add elements with same value
- Print a given matrix in spiral form using direction tracking method
- Find total no of collisions taking place between the balls in which initial direction of each ball is given
- Check if given permutation of 1 to N is feasible using given operations
- Check if the given permutation is a valid BFS of a given Tree
- Check if the array can be sorted only if the elements on given positions can be swapped
- Print direction of moves such that you stay within the [-k, +k] boundary
- Number of comparisons in each direction for m queries in linear search
- Find if there exists a direction for ranges such that no two range intersect
- Distance Traveled by Two Trains together in the same Direction
- Check if the given permutation is a valid DFS of graph
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
- Check if the given array is same as its inverse permutation
- Count array elements that can be maximized by adding any permutation of first N natural numbers
- Check if given array can be made 0 with given operations performed any number of times

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.