PHP Program for Check if an array is sorted and rotated Last Updated : 22 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Given an array of N distinct integers. The task is to write a program 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 at least one rotation.Examples: Input : arr[] = { 3, 4, 5, 1, 2 }Output : YESThe 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[] = {7, 9, 11, 12, 5}Output: YESInput: arr[] = {1, 2, 3}Output: NOInput: arr[] = {3, 4, 6, 1, 2, 5}Output: NOApproach: Find the minimum element in the array.Now, if the array is sorted and then rotate 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 minimum element are in increasing order.Check if all elements after 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 satisfies then print YES otherwise print NO.Below is the implementation of the above idea: 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 checkIfSortRotated($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"); } // Driver code $arr = array(3, 4, 5, 1, 2); //Function Call $n = count($arr); checkIfSortRotated($arr, $n); // This code is contributed // by inder_verma. ?> OutputYESComplexity Analysis: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 an array is sorted and rotated for more details! Comment More infoAdvertise with us Next Article PHP Program for Check if an array is sorted and rotated kartik Follow Improve Article Tags : Web Technologies PHP PHP Programs DSA Arrays rotation +2 More Practice Tags : Arrays Similar Reads PHP Program for Search an element in a sorted and rotated array An element in a sorted array can be found in O(log n) time via binary search. But suppose we rotate an ascending order sorted array at some pivot unknown to you beforehand. So for instance, 1 2 3 4 5 might become 3 4 5 1 2. Devise a way to find an element in the rotated array in O(log n) time.Exampl 6 min read PHP Program to Check if it is possible to sort the array after rotating it Given an array of size N, the task is to determine whether its possible to sort the array or not by just one shuffle. In one shuffle, we can shift some contiguous elements from the end of the array and place it in the front of the array. For example.A = {2, 3, 1, 2}, we can shift {1, 2} from the end 3 min read PHP Program to Sort an Array of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples:Input: [0, 1, 2, 0, 1, 2] Output: [0, 0, 1, 1, 2, 2] Input: [0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1] Output: [0, 0, 0, 0 4 min read PHP Program For Counting Inversions In An Array - Set 1 (Using Merge Sort) Inversion Count for an array indicates - how far (or close) the array is from being sorted. If the array is already sorted, then the inversion count is 0, but if the array is sorted in the reverse order, the inversion count is the maximum. Formally speaking, two elements a[i] and a[j] form an invers 2 min read PHP Program to Split Array and Add First Part to the End | Set 2 Given an array, the task is to split it from a specified position, and move the first part of array and add to the end. Examples: Input : Arr = [ 12, 10, 5, 6, 52, 36 ] k = 2 Output : Arr = [ 5, 6, 52, 36, 12, 10 ] Explanation : Split from index 2 and first part {12, 10} add to the end. Input : Arr 2 min read PHP Program to Sort an Array in Ascending Order Sorting array elements is a common operation in programming. PHP provides several methods to achieve this operation. In this article, we will explore various approaches to sort array elements of an array in ascending order.Table of ContentUsing sort() FunctionUsing asort() FunctionUsing array_multis 3 min read PHP Program to Check horizontal and vertical symmetry in binary matrix Given a 2D binary matrix of N rows and M columns. The task is to check whether the matrix is horizontal symmetric, vertical symmetric, or both. The matrix is said to be horizontal symmetric if the first row is the same as the last row, the second row is the same as the second last row, and so on. An 3 min read PHP Program to check if strings are rotations of each other or not Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1? (eg given s1 = ABCD and s2 = CDAB, return true, given s1 = ABCD, and s2 = ACBD , return false) Algorithm:areRotations(str1, str2): 1. Create a temp string and store concatenation of str1 to str1 in temp. temp = 2 min read PHP Program for Reversal algorithm for right rotation of an array Given an array, right rotate it by k elements. After K=3 rotation Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} k = 3Output: 8 9 10 1 2 3 4 5 6 7Input: arr[] = {121, 232, 33, 43 ,5} k = 2Output: 43 5 121 232 33Note: In the below solution, k is assumed to be smaller than or equal to n. We 2 min read PHP Program to Check if all rows of a matrix are circular rotations of each other Given a matrix of n*n size, the task is to find whether all rows are circular rotations of each other or not. Examples: Input: mat[][] = 1, 2, 3 3, 1, 2 2, 3, 1Output: Yes ,All rows are rotated permutation of each other.Input: mat[3][3] = 1, 2, 3 3, 2, 1 1, 3, 2Output: No, Explanation : As 3, 2, 1 i 2 min read Like