Sunday, June 26, 2022
HomeSoftware DevelopmentCircularly Sorted Array (Sorted and Rotated Array)

Circularly Sorted Array (Sorted and Rotated Array)


Improve Article

Save Article

Like Article

Circularly sorted arrays are arrays that are sorted in ascending or descending order and then rotated by a number of steps.

Let us take an example to know more about circularly sorted arrays:

Consider an array: arr[] = {23, 34, 45, 12, 17, 19}
The elements here, {12, 17, 19, 23, 34, 45} are sorted ‘In-order’ but they are rotated to the left by 3 times.

Ascending circularly sorted array:

  • Consider an array sorted in ascending order: arr[] = {12, 17, 19, 23, 34, 45} 
  • If the above array is rotated by 3 steps to the left, then the resultant array will be ascending circularly sorted array: {23, 34, 45, 12, 17, 19}

Descending circularly sorted array:

  • Consider an array sorted in descending order: arr[] = {35, 26, 11, 9, 5, 3} 
  • If the above array is rotated by 3 steps to the left, then the resultant array will be descending circularly sorted array: {9, 5, 3, 35, 26, 11}

Let us check through the below problem whether the given array is circularly sorted or not:

Problem Statement:

Given an array arr[] of length N, the task is to check whether the given array is circularly sorted or not, we need to check whether the given array is the rotated form of the sorted array. 

  • In ascending circularly sorted array, there will be at most one case where the element just before the current element will be greater than the current element i.e., arr[ i – 1 ] > arr[ i ]. So we need to count the total existence of such cases and if the count is greater than 1 then the result will be false else the result will be true meaning that the array is circularly sorted.

Below is the implementation for the above approach: 

C++

  

#include <bits/stdc++.h>

using namespace std;

  

bool checkCircularSorted(int arr[], int n)

{

  

    

    

    int cnt = 0;

  

    for (int i = 1; i < n; i++) {

  

        

        if (arr[i - 1] > arr[i]) {

            cnt++;

        }

    }

  

    

    

    if (cnt > 1) {

        return false;

    }

    else {

        return true;

    }

}

  

int main()

{

  

    

    int arr[] = { 23, 34, 45, 12, 17, 19 };

  

    

    int N = sizeof(arr) / sizeof(arr[0]);

  

    

    

    bool result = checkCircularSorted(arr, N);

  

    

    if (result) {

        cout << "Array is circularly sorted";

    }

    else {

        cout << "Array is not circularly sorted";

    }

}

Java

  

public class Main {

  

    

    

    public static boolean checkCircularSorted(int arr[])

    {

        

        

        int cnt = 0;

  

        for (int i = 1; i < arr.length; i++) {

            

            if (arr[i - 1] > arr[i]) {

                cnt++;

            }

        }

        

        

        if (cnt > 1) {

            return false;

        }

        else {

            return true;

        }

    }

  

    

    public static void main(String args[])

    {

        

        int arr[] = { 23, 34, 45, 12, 17, 19 };

  

        

        

        boolean result = checkCircularSorted(arr);

  

        

        if (result == true) {

            System.out.println(

                "Array is circularly sorted");

        }

        else {

            System.out.println(

                "Array is not circularly sorted");

        }

    }

}

Output

Array is circularly sorted

Similarly, we can do the above operation for descending circularly sorted array. In this case we need to consider the count of the case where, arr [i-1] < arr[i]

Related articles:

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments