Java Program for Check if an array is sorted and rotated Last Updated : 27 May, 2022 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 : 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[] = {7, 9, 11, 12, 5} Output: YES Input: arr[] = {1, 2, 3} Output: NO Input: arr[] = {3, 4, 6, 1, 2, 5} Output: NO Approach: 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: Java // Java program to check if an // array is sorted and rotated // clockwise import java.io.*; class GFG { // Function to check if an array is // sorted and rotated clockwise static void checkIfSortRotated(int arr[], int n) { int minEle = Integer.MAX_VALUE; int maxEle = Integer.MIN_VALUE; int minIndex = -1; // Find the minimum element // and it's index for (int i = 0; i < n; i++) { if (arr[i] < minEle) { minEle = arr[i]; minIndex = i; } } boolean flag1 = true; // Check if all elements before // minIndex are in increasing order for (int i = 1; i < minIndex; i++) { if (arr[i] < arr[i - 1]) { flag1 = false; break; } } boolean flag2 = true; // Check if all elements after // minIndex are in increasing order for (int i = minIndex + 1; i < n; i++) { if (arr[i] < arr[i - 1]) { flag2 = false; break; } } // check if the minIndex is 0, i.e the first element // is the smallest , which is the case when array is // sorted but not rotated. if (minIndex == 0) { System.out.print("NO"); return; } // Check if last element of the array // is smaller than the element just // before the element at minIndex // 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])) System.out.println("YES"); else System.out.print("NO"); } // Driver code public static void main(String[] args) { int arr[] = { 3, 4, 5, 1, 2 }; int n = arr.length; // Function Call checkIfSortRotated(arr, n); } } // This code is contributed // by inder_verma. OutputYES 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 Java Program for Check if an array is sorted and rotated kartik Follow Improve Article Tags : Java Java Programs DSA Arrays rotation +1 More Practice Tags : ArraysJava Similar Reads Java 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. Examp 9 min read Check if an array is sorted and rotated using Binary Search Pre-requisite: Check if an array is sorted and rotated using Linear SearchGiven an array arr[] of N distinct integers, the task is to check if this array is sorted when rotated counter-clockwise. A sorted array is not considered sorted and rotated, i.e., there should at least one rotation. Examples: 11 min read Java Program for Given a sorted and rotated array, find if there is a pair with a given sum Given an array that is sorted and then rotated around an unknown point. Find if the array has a pair with a given sum 'x'. It may be assumed that all elements in the array are distinct. Examples : Input: arr[] = {11, 15, 6, 8, 9, 10}, x = 16 Output: true There is a pair (6, 10) with sum 16 Input: ar 6 min read Java 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 eg: A = {2, 3, 1, 2}, we can shift {1, 2} from the end of t 3 min read Java Program to Sort an array in wave form Given an unsorted array of integers, sort the array into a wave-like array. An array 'arr[0..n-1]' is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= ..... Examples: Input: arr[] = {10, 5, 6, 3, 2, 20, 100, 80} Output: arr[] = {10, 5, 6, 2, 20, 3, 100, 80} OR 4 min read Java Program For Sorting 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, 6 min read Java Program to Left Rotate the Elements of an Array In Java, left rotation of an array involves shifting its elements to the left by a given number of positions, with the first elements moving around to the end. There are different ways to left rotate the elements of an array in Java.Example: We can use a temporary array to rotate the array left by " 5 min read How to Check if One String is a Rotation of Another in Java? In Java, to check if one string is a rotation of another, we can do string concatenation and substring matching. A rotated string is created by moving some characters from the beginning of the string to the end while maintaining the character order.Program to Check if One String is a Rotation of Ano 2 min read Java 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 horizontally symmetric, vertically symmetric, or both. The matrix is said to be horizontally 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 4 min read Java Program to Sort 2D Array Across Columns The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in the java.util package and implements the List interface, so we can use all the methods of List interface here. This program is used to Sor 3 min read Like