Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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. We try all possible rotations and check if any rotation produces a sorted array.
Example
Let's examine two test cases:
- Input: [3, 4, 5, 6, 1, 2] ? Output: Yes (after 2 right rotations: [1, 2, 3, 4, 5, 6])
- Input: [5, 1, 6, 2, 5, 3] ? Output: No (cannot be made sorted with any rotation)
// function to rotate the given array to the right by one position
function rotate(arr) {
var l = 0;
var r = arr.length - 1;
while (l 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;
var originalArr = [...arr]; // preserve original array
while (k--) {
if (increasing(arr) || decreasing(arr)) {
return true;
}
arr = rotate(arr);
}
return false;
}
// defining the arrays
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");
}
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
Time Complexity: O(N²) - We check N rotations, each taking O(N) time
Space Complexity: O(1) - Only using a few variables
Efficient Approach
Instead of checking all rotations, we can determine if a rotation exists by finding at most one "break point" in the increasing/decreasing pattern. If there's exactly one break point, we check if rotating at that point creates a valid sequence.
Algorithm Logic
For an array to be rotatable into increasing order:
- Find the first position where arr[i] > arr[i+1] (break point)
- If no break point exists, array is already sorted
- If one break point exists, check if the remaining elements are sorted and if arr[last] ? arr[0]
- If multiple break points exist, rotation is impossible
// function to check if the given array can be made increasing by rotation
function canMakeIncreasing(arr) {
var len = arr.length;
var breakPoint = -1;
// Find the break point where increasing order is violated
for (var i = 0; i arr[i + 1]) {
if (breakPoint == -1) {
breakPoint = i;
} else {
// More than one break point - impossible to fix with rotation
return false;
}
}
}
// No break point - already increasing
if (breakPoint == -1) return true;
// Check if rotating at break point creates valid sequence
return arr[len - 1] = arr[0];
}
// function to check whether the given array can become increasing or decreasing after rotation
function checkEfficient(arr) {
return canMakeIncreasing(arr) || canMakeDecreasing(arr);
}
// Test cases
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 (checkEfficient(arr1)) {
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 (checkEfficient(arr2)) {
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");
}
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
Time Complexity: O(N) - Single pass through the array
Space Complexity: O(1) - Only using a few variables
Comparison
| Approach | Time Complexity | Space Complexity | Logic |
|---|---|---|---|
| Naive | O(N²) | O(1) | Try all rotations |
| Efficient | O(N) | O(1) | Find break points |
Conclusion
We implemented two approaches to check if an array can be made increasing or decreasing by rotation. The efficient approach with O(N) time complexity uses the insight that a rotatable array has at most one break point in its sorted order.
