How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
🧠 Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
📖 Select Passive to activate
You are given an array nums consisting of integers. A pair of consecutive integers (nums[i], nums[i+1]) is called a special pair if both integers belong to the same "team":
The task is to process several queries. Each query is of the form [left, right], and you need to determine whether there are no special pairs between indices left and right in the array. If there are no special pairs, return true for the query; otherwise, return false.
We create a prefix array prefix that counts the cumulative number of special pairs as we traverse the array:
For example, for nums = [4, 3, 1, 6]:
0: No pairs yet → prefix[0] = 0.1: (4, 3) isn’t special → prefix[1] = 0.2: (3, 1) is special → prefix[2] = 1.3: (1, 6) isn’t special → prefix[3] = 1.Final prefix array: [0, 0, 1, 1].
For each query [left, right]:
specialCount = prefix[right] - (left > 0 ? prefix[left - 1] : 0);
specialCount == 0, return true; otherwise, return false.O(n), where n is the size of nums.O(q), where q is the number of queries.Total: O(n + q).
Input:
nums = [4, 3, 1, 6];
queries = [[0, 1], [1, 2], [0, 3]];
Output:
[true, false, false]
Explanation:
[0, 1]: No special pairs → true.[1, 2]: (3, 1) is special → false.[0, 3]: (3, 1) is special → false.Input:
nums = [2, 4, 6, 8];
queries = [[0, 3], [1, 2]];
Output:
[false, false]
Explanation:
[0, 3]: All pairs are special → false.[1, 2]: (4, 6) is special → false.Input:
nums = [1, 3, 5, 7];
queries = [[0, 2], [1, 3]];
Output:
[false, false]
Explanation:
[0, 2]: All pairs are special → false.[1, 3]: All pairs are special → false.🎯 Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
🔒 Locked steps cannot be moved. Only unlocked steps are draggable.
🔊 Enable sound for swap feedback and completion effects.