📌  相关文章
📜  检查给定的1到N的排列是否可以按顺时针或逆时针方向计数

📅  最后修改于: 2021-05-17 23:43:33             🧑  作者: Mango

给定大小为N的整数数组arr ,其中包含从1N的不同元素。任务是检查是否可以找到数组中的位置,以便可以按顺时针方向或逆时针方向对从1N的所有数字进行计数。
例子:

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.

方法:以上问题可以通过观察和分析解决。

  1. 在阵列中的索引可以发现仅当连续元素大于1之间的绝对差值的计数正好是1,因为仅然后,将有可能从1数到N的顺时针或逆时针顺序。
  2. 如果相邻元素之间的绝对差计数大于1 ,则将不可能按顺时针或逆时针顺序从1N计数。

以下是上述方法的基本实现:

C++
// C++ program to check Clockwise or
// counterclockwise order in an array
  
#include 
using namespace std;
  
bool check_order(vector 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 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


输出:
YES

时间复杂度: O(N)
辅助空间: O(1)