Check if given permutation of 1 to N can be counted in clockwise or anticlockwise direction

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: YES
Explanation:
                    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: NO
Explanation: 
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.

  1. 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.
  2. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

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.



Improved By : vishu2908, amit143katiyar

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.