Skip to content
Related Articles

Related Articles

Check if given permutation of 1 to N can be counted in clockwise or anticlockwise direction
  • Last Updated : 03 May, 2021

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 a 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++




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

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

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.

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

Javascript




<script>
 
// Javascript program to check clockwise or
// counterclockwise order in an array   
function check_order(arr)
{
        var cnt = 0;
        for (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
     
        var arr = [ 2, 3, 4, 5, 1 ];
 
        if (check_order(arr))
            document.write("YES");
        else
            document.write("NO");
 
// This code contributed by gauravrajput1
 
</script>
Output: 
YES

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :