Java Program for Check if it is possible to make array increasing or decreasing by rotating the array

Given an array arr[] of N distinct elements, the task is to check if it is possible to make the array increasing or decreasing by rotating the array in any direction.
Examples:Â
Â
Input: arr[] = {4, 5, 6, 2, 3}Â
Output: YesÂ
Array can be rotated as {2, 3, 4, 5, 6}
Input: arr[] = {1, 2, 4, 3, 5}Â
Output: NoÂ
Â
Â
Approach: There are four possibilities:Â
Â
- If the array is already increasing then the answer is Yes.
- If the array is already decreasing then the answer is Yes.
- If the array can be made increasing, this can be possible if the given array is first increasing up to the maximum element and then decreasing.
- If the array can be made decreasing, this can be possible if the given array is first decreasing up to the minimum element and then increasing.
If it is not possible to make the array increasing or decreasing then print No.
Below is the implementation of the above approach:Â
Â
Java
// Java implementation of the approachclass GFG{Â Â Â Â Â // Function that returns true if the array// can be made increasing or decreasing// after rotating it in any directionstatic 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 increasing or decreasing    return false;}Â
// Driver codepublic 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
Â
Time Complexity: O(n), where n represents the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Please refer complete article on Check if it is possible to make array increasing or decreasing by rotating the array for more details!
Â



