PHP Program to Check if it is possible to sort the array after rotating it Last Updated : 23 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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 of the array to the front of the array to sort it.A = {1, 2, 3, 2} since we cannot sort it in one shuffle hence it's not possible to sort the array.Examples: Input: arr[] = {1, 2, 3, 4} Output: Possible Since this array is already sorted hence no need for shuffle.Input: arr[] = {6, 8, 1, 2, 5}Output: PossiblePlace last three elements at the front in the same order i.e. {1, 2, 5, 6, 8}Approach: Check if the array is already sorted or not. If yes return true.Else start traversing the array elements until the current element is smaller than the next element. Store that index where arr[i] > arr[i+1].Traverse from that point and check if from that index elements are in increasing order or not.If above both conditions are satisfied then check if the last element is smaller than or equal to the first element of the given array.Print "Possible" if the above three conditions are satisfied else print "Not possible" if any of the above 3 conditions failed.Below is the implementation of the above approach: PHP // PHP implementation of // above approach // Function to check if // it is possible function is_sorted($a, $n) { $c1 = 0; $c2 = 0; // if array is ascending for($i = 0; $i < $n - 1; $i++) { if($a[$i] <= $a[$i + 1]) $c1++; } // if array is descending for($i = 1; $i < $n; $i++) { if($a[$i] <= $a[$i - 1]) $c2++; } if($c1 == $n || $c2 == $n) return true; return false; } function isPossible($a, $n) { // step 1 if (is_sorted($a, $n)) { echo "Possible" . " "; } else { // break where a[i] > a[i+1] $flag = true; $i; for ($i = 0; $i < $n - 1; $i++) { if ($a[$i] > $a[$i + 1]) { break; } } // break point + 1 $i++; // check whether the sequence is // further increasing or not for ($k = $i; $k < $n - 1; $k++) { if ($a[$k] > $a[$k + 1]) { $flag = false; break; } } // If not increasing after // break point if (!$flag) return false; else { // last element <= First element if ($a[$n - 1] <= $a[0]) return true; else return false; } } } // Driver code $arr = array( 3, 1, 2, 2, 3 ); $n = sizeof($arr); if (isPossible($arr, $n)) echo "Possible"; else echo "Not Possible"; // This code is contributed // by Akanksha Rai(Abby_akku) ?> OutputPossibleComplexity Analysis: Time Complexity: O(n)Auxiliary Space: O(1)Please refer complete article on Check if it is possible to sort the array after rotating it for more details! Comment More infoAdvertise with us Next Article PHP Program to Check if it is possible to sort the array after rotating it kartik Follow Improve Article Tags : Greedy Sorting Competitive Programming Web Technologies PHP PHP Programs DSA rotation +4 More Practice Tags : GreedySorting Similar Reads PHP Program for Check if an array is sorted and rotated 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 arra 3 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 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 to Check if two numbers are bit rotations of each other or not Given two positive integers x and y, check if one integer is obtained by rotating bits of other. Input constraint: 0 < x, y < 2^32 Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end.More info 3 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 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 Find element at given index after a number of rotations An array consisting of N integers is given. There are several Right Circular Rotations of range[L..R] that we perform. After performing these rotations, we need to find an element at a given index.Examples : Input : arr[] : {1, 2, 3, 4, 5} ranges[] = { {0, 2}, {0, 3} } index : 1 Output : 3 Explanati 3 min read PHP Program for Array Left Rotation by d Positions Given an array, the task is to rotate an array to the left by d position in PHP. Array left rotation by d positions involves shifting array elements to the left by d positions.Examples:Input: arr = {1, 2, 3, 4, 5, 6, 7}, d = 2Output: 3 4 5 6 7 1 2Input: arr = {3, 4, 5, 6, 7, 1, 2}, d = 2Output: 5 6 3 min read How to check an array is multidimensional or not in PHP ? Given an array (single-dimensional or multi-dimensional) the task is to check whether the given array is multi-dimensional or not. Below are the methods to check if an array is multidimensional or not in PHP:Table of ContentUsing rsort() functionUsing Nested foreach LoopUsing a Recursive FunctionUsi 4 min read PHP Program to Sort an Array Elements in Descending Order Given an array containing some elements, the task is to sort the array elements in descending order in PHP. Sorting elements of an array is a common operation in programming, and PHP provides several methods to accomplish this task.Table of ContentUsing rsort() FunctionUsing array_reverse() with sor 3 min read Like