# JavaScript Program to Check if it is possible to make array increasing or decreasing by rotating the array

Rotation of the array means to assume the array as a circular array and rotate the elements of the array to either their left or right direction by one index at each rotation and the element at one end may take the value present at another end. An Increasing array means that each element will be greater than or equal to its previous element and decreasing array means each element will be less than or equal to the previous element.

In this problem, we are given an array and we can rotate the array in either the left or the right direction and we have to find whether after certain rotations (possibly zero) can we make the array increasing or decreasing.

## Naive Approach

In this approach we will rotate the array and for each rotation we will check whether the current array is increasing or decreasing.

### Example

In the below example, we check if it is possible to make the given array increasing or decreasing by rotating it. Below are the inputs and expected outputs.

Input: arr = [3, 4, 5, 6, 1, 2]

Expected Output: Yes

Input: arr = [ 5, 1, 6, 2, 5, 3 ]

Expected Output: No

### Example

// function to rotate the given array
function rotate(arr){
var l = 0;
var r = arr.length-1;
while(l < r){
arr[l] += arr[r];
arr[r] = arr[l]-arr[r];
arr[l] = arr[l]-arr[r];
l++;
}
return arr;
}

// function to check if the given array is increasing or not
function increasing(arr){

// getting the size of array
var len = arr.length

// traversing over the array
for(var i = 1; i < len; i++){
if(arr[i] < arr[i-1]){
return false;
}
}
return true;
}

// function to check if the given array is decreasing or not
function decreasing(arr){

// getting the size of array
var len = arr.length

// traversing over the array
for(var i = 1; i < len; i++){
if(arr[i] > arr[i-1]){
return false;
}
}
return true;
}

// function to check whether the given array can become
// increasing or decreasing after certain rotations
function check(arr){
var k = arr.length
while(k--){
if(increasing(arr) || decreasing(arr)){
return true;
}
arr = rotate(arr);
}
return false;
}

// defining the arr's
var arr1 = [3, 4, 5, 6, 1, 2]
var arr2 = [5, 1, 6, 2, 5, 3]
console.log("The given array is: ");
console.log(arr1)
if(check(arr1) == true){
console.log("Yes, after some rotations given array can be transformed into an increasing or decreasing array");
}
else{
console.log("No, after some rotations given array cannot be transformed into an increasing or decreasing array");
}
console.log("The given array is: ");
console.log(arr2)
if(check(arr2) == true){
console.log("Yes, after some rotations given array can be transformed into an increasing or decreasing array");
}
else{
console.log("No, after some rotations given array cannot be transformed into an increasing or decreasing array");
}


### Output

The given array is:
[ 3, 4, 5, 6, 1, 2 ]
Yes, after some rotations given array can be transformed into an increasing or decreasing array
The given array is:
[ 5, 1, 6, 2, 5, 3 ]
No, after some rotations given array cannot be transformed into an increasing or decreasing array


The time complexity of the above code is O(N*N) and the space complexity is O(1).

## Efficient Approach

In the previous array we have checked for each rotation whether the array is increasing or decreasing, in this approach we will partially check for the increasing or decreasing array.

### Example

// function to check if the given array is increasing or not
function increasing(arr){

// getting the size of array
var len = arr.length

// traversing over the array
var i = 0;
for(var i = 1; i < len; i++){
if(arr[i] < arr[i-1]){
break;
}
}
if(i == len) return true;
i++;
for(; i< len; i++){
if(arr[i] < arr[i-1]){
return false;
}
}
return arr[len-1] <= arr;
}

// function to check if the given array is decreasing or not
function decreasing(arr){

// getting the size of array
var len = arr.length

// traversing over the array
var i = 0;
for(var i = 1; i < len; i++){
if(arr[i] > arr[i-1]){
break;
}
}
if(i == len) return true;
i++;
for(; i< len; i++){
if(arr[i] > arr[i-1]){
return false;
}
}
return arr[len-1] >= arr;
}

// function to check whether the given array can become increasing or decreasing after certain rotations
function check(arr){
if(increasing(arr) || decreasing(arr)){
return true;
}
else{
return false;
}
}

// defining the arr's
var arr1 = [3, 4, 7, 6, 1, 2]
var arr2 = [5, 1, 6, 2, 5, 3]
console.log("The given array is: ");
console.log(arr1)
if(check(arr1) == true){
console.log("Yes, after some rotations given array can be transformed into an increasing or decreasing array");
}
else{
console.log("No, after some rotations given array cannot be transformed into an increasing or decreasing array");
}
console.log("The given array is: ");
console.log(arr2)
if(check(arr2) == true){
console.log("Yes, after some rotations given array can be transformed into an increasing or decreasing array");
}
else{
console.log("No, after some rotations given array cannot be transformed into an increasing or decreasing array");
}


### Output

The given array is:
[ 3, 4, 7, 6, 1, 2 ]
No, after some rotations given array cannot be transformed into an increasing or decreasing array
The given array is:
[ 5, 1, 6, 2, 5, 3 ]
No, after some rotations given array cannot be transformed into an increasing or decreasing array


The time complexity of the above code is O(N) and the space complexity is O(1).

## Conclusion

In this tutorial, we have implemented a JavaScript program for check if it is possible to make the given array increasing or decreasing by rotating it. We have implemented two approaches with O(N*N) and O(N) time complexity and both with O(1) space complexity.

Updated on: 13-Apr-2023

50 Views 