Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmC++ Program to Check if it is possible to sort the array...

C++ 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: 
 

  1. 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.
  2. 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: Possible
Place last three elements at the front 
in the same order i.e. {1, 2, 5, 6, 8}

 

Approach: 
 

  1. Check if the array is already sorted or not. If yes return true.
  2. Else start traversing the array elements until the current element is smaller than next element. Store that index where arr[i] > arr[i+1].
  3. Traverse from that point and check if from that index elements are in increasing order or not.
  4. If above both conditions satisfied then check if last element is smaller than or equal to the first element of given array.
  5. Print “Possible” if above three conditions satisfied else print “Not possible” if any of the above 3 conditions failed.

Below is the implementation of above approach: 
 

C++




// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if it is possible
bool isPossible(int a[], int n)
{
    // step 1
    if (is_sorted(a, a + n)) {
        cout << "Possible" << endl;
    }
 
    else {
 
        // break where a[i] > a[i+1]
        bool flag = true;
        int 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 (int 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
int main()
{
 
    int arr[] = { 3, 1, 2, 2, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    if (isPossible(arr, n))
        cout << "Possible";
 
    else
        cout << "Not Possible";
 
    return 0;
}


Output: 

Possible

 

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!
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
03 Mar, 2022
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments