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
Converting array into increasing sequence in JavaScript
An increasing sequence in JavaScript is an array where each element is less than or equal to the next element. This article shows how to determine if an array can be converted to an increasing sequence by modifying at most one element.
Problem Definition
We define an array as increasing if arr[i] <= arr[i + 1] holds for every index i where (0 <= i <= n - 2).
Given an array of integers, we need to determine whether we can convert it into an increasing array by modifying at most one element. The function should return true if possible, false otherwise.
Example Input and Output
For the input array [8, 3, 3, 7, 9], we can replace the 8 at index 0 with 1 or 2 to get an increasing sequence like [1, 3, 3, 7, 9].
const arr = [8, 3, 3, 7, 9];
console.log("Original array:", arr);
console.log("Can be converted:", true);
console.log("Example result: [1, 3, 3, 7, 9]");
Original array: [ 8, 3, 3, 7, 9 ] Can be converted: true Example result: [1, 3, 3, 7, 9]
Solution Implementation
const arr = [8, 3, 3, 7, 9];
const canConvert = (arr = []) => {
const isIncreasing = (array) => {
for (let i = 1; i < array.length; i++) {
if (array[i] < array[i - 1]) {
return false;
}
}
return true;
};
// If already increasing, no modification needed
if (isIncreasing(arr)) {
return true;
}
// Try modifying each element
for (let i = 0; i < arr.length; i++) {
const original = arr[i];
// Try setting current element to previous element's value
if (i > 0) {
arr[i] = arr[i - 1];
if (isIncreasing(arr)) {
arr[i] = original; // restore
return true;
}
arr[i] = original; // restore
}
// Try setting current element to next element's value
if (i < arr.length - 1) {
arr[i] = arr[i + 1];
if (isIncreasing(arr)) {
arr[i] = original; // restore
return true;
}
arr[i] = original; // restore
}
}
return false;
};
console.log("Input:", arr);
console.log("Can convert to increasing sequence:", canConvert(arr));
Input: [ 8, 3, 3, 7, 9 ] Can convert to increasing sequence: true
Testing with Different Cases
const testCases = [
[8, 3, 3, 7, 9], // true - can modify first element
[1, 2, 3, 4, 5], // true - already increasing
[5, 4, 3, 2, 1], // false - needs more than one change
[1, 3, 2, 4], // true - can modify middle element
[1, 1, 1] // true - already non-decreasing
];
const canConvert = (arr = []) => {
const isIncreasing = (array) => {
for (let i = 1; i < array.length; i++) {
if (array[i] < array[i - 1]) {
return false;
}
}
return true;
};
if (isIncreasing(arr)) return true;
for (let i = 0; i < arr.length; i++) {
const original = arr[i];
if (i > 0) {
arr[i] = arr[i - 1];
if (isIncreasing(arr)) {
arr[i] = original;
return true;
}
arr[i] = original;
}
if (i < arr.length - 1) {
arr[i] = arr[i + 1];
if (isIncreasing(arr)) {
arr[i] = original;
return true;
}
arr[i] = original;
}
}
return false;
};
testCases.forEach((testArr, index) => {
console.log(`Test ${index + 1}: [${testArr}] -> ${canConvert([...testArr])}`);
});
Test 1: [8,3,3,7,9] -> true Test 2: [1,2,3,4,5] -> true Test 3: [5,4,3,2,1] -> false Test 4: [1,3,2,4] -> true Test 5: [1,1,1] -> true
How It Works
The algorithm works by:
- First checking if the array is already increasing
- Then trying to modify each element by setting it to either its previous or next element's value
- Testing if the modified array becomes increasing
- Restoring the original value after each test
Conclusion
This solution efficiently determines if an array can become increasing with at most one modification. The algorithm tests each possible single modification and checks if the result is a valid increasing sequence.
