Making two sequences increasing in JavaScript

In JavaScript, making two sequences strictly increasing by swapping elements at the same indices is a dynamic programming problem. A sequence is strictly increasing if each element is greater than the previous one.

Problem Statement

Given two arrays arr1 and arr2, we can swap elements at any index i between the arrays. The goal is to find the minimum number of swaps needed to make both arrays strictly increasing.

Understanding the Example

Consider the input arrays:

const arr1 = [1, 3, 5, 4];
const arr2 = [1, 2, 3, 7];

console.log("Original arrays:");
console.log("arr1:", arr1);
console.log("arr2:", arr2);
Original arrays:
arr1: [ 1, 3, 5, 4 ]
arr2: [ 1, 2, 3, 7 ]

Array arr1 is not strictly increasing because 5 > 4. By swapping elements at index 3, we get [1, 3, 5, 7] and [1, 2, 3, 4], both strictly increasing.

Dynamic Programming Solution

We use dynamic programming to track the minimum swaps needed. For each position, we maintain two states: swapped or not swapped at the current index.

const findSwaps = (arr1 = [], arr2 = []) => {
    // Map to store minimum swaps for current position
    // true: swapped at current index, false: not swapped
    let map = {
        true: 1,   // Start with 1 swap if we swap at index 0
        false: 0,  // Start with 0 swaps if we don't swap at index 0
    };
    
    for (let i = 1; i < arr1.length; i++) {
        const current = {
            true: Infinity,
            false: Infinity,
        };
        
        // Check if we can swap at current position
        if (arr1[i] > arr2[i - 1] && arr2[i] > arr1[i - 1]) {
            // We can swap: arr1[i] goes to arr2, arr2[i] goes to arr1
            current.true = Math.min(current.true, map.false + 1);
            current.false = Math.min(current.false, map.true);
        }
        
        // Check if we can keep elements at current position
        if (arr2[i] > arr2[i - 1] && arr1[i] > arr1[i - 1]) {
            // We can keep: both arrays remain increasing
            current.true = Math.min(current.true, map.true + 1);
            current.false = Math.min(current.false, map.false);
        }
        
        map = current;
    }
    
    return Math.min(map.false, map.true);
};

const arr1 = [1, 3, 5, 4];
const arr2 = [1, 2, 3, 7];

console.log("Minimum swaps needed:", findSwaps(arr1, arr2));
Minimum swaps needed: 1

How the Algorithm Works

The algorithm considers two scenarios at each position:

  1. Cross-swap scenario: Check if swapping creates valid increasing sequences
  2. No-swap scenario: Check if keeping elements maintains increasing sequences
// Test with another example
const testArr1 = [0, 4, 4, 5, 9];
const testArr2 = [0, 1, 6, 8, 10];

console.log("Test arrays:");
console.log("arr1:", testArr1);
console.log("arr2:", testArr2);
console.log("Minimum swaps:", findSwaps(testArr1, testArr2));
Test arrays:
arr1: [ 0, 4, 4, 5, 9 ]
arr2: [ 0, 1, 6, 8, 10 ]
Minimum swaps: 1

Key Points

  • Dynamic programming tracks minimum swaps for each position
  • Two states are maintained: swapped or not swapped at current index
  • Algorithm checks both cross-swap and no-swap possibilities
  • Time complexity: O(n), Space complexity: O(1)

Conclusion

This dynamic programming approach efficiently finds the minimum swaps needed to make both sequences strictly increasing. The algorithm considers all valid swap combinations and returns the optimal solution.

Updated on: 2026-03-15T23:19:00+05:30

264 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements