The function is to check if this array is sorted and rotated counter-clockwise. A sorted array is not considered as sorted and rotated, i.e., there should be at least one rotation.

Examples Below:

``````Input : arr[] = { 3, 4, 5, 1, 2 }
Output : YES
The above array is sorted and rotated.
Sorted array: {1, 2, 3, 4, 5}.
Rotating this sorted array clockwise
by 3 positions, we get: { 3, 4, 5, 1, 2}

Input: arr[] = {3, 4, 6, 9, 2}
Output: YES

Input: arr[] = {10, 8, 10, 1, 2}
Output: NO``````

#### Condition Approach:

• Find the minimum element in the array.
• if the array is sorted and rotated all the elements before the minimum element will be in increasing order and all elements after the minimum element will also be in increasing order.
• Check if all elements before the minimum element are in increasing order.
• Check if all elements after the minimum element are in increasing order.
• Check if the last element of the array is smaller than the starting element.
• If all of the above three conditions are satisfied then print YES otherwise print NO.

PHP

``````<?php
// PHP program to check if an
// array is sorted and rotated
// clockwise

// Function to check if an array
// is sorted and rotated clockwise
function checkArrIfSortRotated(\$arr, \$n)
{
\$minEle = PHP_INT_MAX;
\$maxEle = PHP_INT_MIN;

\$minIndex = -1;

// Find the minimum element
// and it's index
for (\$i = 0; \$i <\$n; \$i++)
{
if (\$arr[\$i] < \$minEle)
{
\$minEle = \$arr[\$i];
\$minIndex = \$i;
}
}

\$flag1 = 1;

// Check if all elements before
// minIndex are in increasing order
for ( \$i = 1; \$i <\$minIndex; \$i++)
{
if (\$arr[\$i] < \$arr[\$i - 1])
{
\$flag1 = 0;
break;
}
}

\$flag2 = 1;

// Check if all elements after
// minIndex are in increasing order
for (\$i = \$minIndex + 1; \$i <\$n; \$i++)
{
if (\$arr[\$i] < \$arr[\$i - 1])
{
\$flag2 = 0;
break;
}
}

// Check if last element of the array
// is smaller than the element just
// starting element of the array
// for arrays like [3,4,6,1,2,5] - not sorted circular array
if (\$flag1 && \$flag2 &&
(\$arr[\$n - 1] < \$arr[0]))
echo( "YES");
else
echo( "NO");
}

//array
\$arr = array(10, 8, 10, 1, 2);

//counter
\$n = count(\$arr);

//call the funtion
checkArrIfSortRotated(\$arr, \$n) //No

?>
``````