📌  相关文章
📜  Java程序检查是否可以通过旋转数组来增加或减少数组

📅  最后修改于: 2022-05-13 01:54:56.834000             🧑  作者: Mango

Java程序检查是否可以通过旋转数组来增加或减少数组

给定一个包含N个不同元素的数组arr[] ,任务是检查是否可以通过沿任意方向旋转数组来使数组增加或减少。
例子:

方法:有四种可能:

  • 如果数组已经在增加,那么答案是Yes
  • 如果数组已经在减少,那么答案是Yes
  • 如果可以使数组增加,则如果给定数组首先增加到最大元素然后减少,则这是可能的。
  • 如果可以使数组减少,则如果给定数组首先减少到最小元素然后增加,则可以做到这一点。

如果无法使数组增加或减少,则打印No
下面是上述方法的实现:

Java
// Java implementation of the approach
class GFG
{
     
// Function that returns true if the array
// can be made increasing or decreasing
// after rotating it in any direction
static boolean isPossible(int a[], int n)
{
    // If size of the array is less than 3
    if (n <= 2)
        return true;
 
    int flag = 0;
     
    // Check if the array is already decreasing
    for (int i = 0; i < n - 2; i++)
    {
        if (!(a[i] > a[i + 1] &&
              a[i + 1] > a[i + 2]))
        {
            flag = 1;
            break;
        }
    }
 
    // If the array is already decreasing
    if (flag == 0)
        return true;
 
    flag = 0;
     
    // Check if the array is already increasing
    for (int i = 0; i < n - 2; i++)
    {
        if (!(a[i] < a[i + 1] &&
              a[i + 1] < a[i + 2]))
        {
            flag = 1;
            break;
        }
    }
 
    // If the array is already increasing
    if (flag == 0)
        return true;
 
    // Find the indices of the minimum
    // && the maximum value
    int val1 = Integer.MAX_VALUE, mini = -1,
        val2 = Integer.MIN_VALUE, maxi = 0;
    for (int i = 0; i < n; i++)
    {
        if (a[i] < val1)
        {
            mini = i;
            val1 = a[i];
        }
        if (a[i] > val2)
        {
            maxi = i;
            val2 = a[i];
        }
    }
 
    flag = 1;
     
    // Check if we can make array increasing
    for (int i = 0; i < maxi; i++)
    {
        if (a[i] > a[i + 1])
        {
            flag = 0;
            break;
        }
    }
 
    // If the array is increasing upto max index
    // && minimum element is right to maximum
    if (flag == 1 && maxi + 1 == mini)
    {
        flag = 1;
         
        // Check if array increasing again or not
        for (int i = mini; i < n - 1; i++)
        {
            if (a[i] > a[i + 1])
            {
                flag = 0;
                break;
            }
        }
        if (flag == 1)
            return true;
    }
 
    flag = 1;
     
    // Check if we can make array decreasing
    for (int i = 0; i < mini; i++)
    {
        if (a[i] < a[i + 1])
        {
            flag = 0;
            break;
        }
    }
 
    // If the array is decreasing upto min index
    // && minimum element is left to maximum
    if (flag == 1 && maxi - 1 == mini)
    {
        flag = 1;
 
        // Check if array decreasing again or not
        for (int i = maxi; i < n - 1; i++)
        {
            if (a[i] < a[i + 1])
            {
                flag = 0;
                break;
            }
        }
        if (flag == 1)
            return true;
    }
 
    // If it is not possible to make the
    // array inreasing or decreasing
    return false;
}
 
// Driver code
public static void main(String args[])
{
    int a[] = { 4, 5, 6, 2, 3 };
    int n = a.length;
 
    if (isPossible(a, n))
        System.out.println( "Yes");
    else
        System.out.println( "No");
}
}
 
// This code is contributed by Arnab Kundu


输出:
Yes

时间复杂度: O(n)

辅助空间: 1

请参阅完整的文章检查是否可以通过旋转数组来增加或减少数组以获取更多详细信息!