Find the Minimum and Maximum Number of Nodes Between Critical Points - Problem

A critical point in a linked list is defined as either a local maxima or a local minima.

A node is a local maxima if the current node has a value strictly greater than the previous node and the next node.

A node is a local minima if the current node has a value strictly smaller than the previous node and the next node.

Note that a node can only be a local maxima/minima if there exists both a previous node and a next node.

Given a linked list head, return an array of length 2 containing [minDistance, maxDistance] where minDistance is the minimum distance between any two distinct critical points and maxDistance is the maximum distance between any two distinct critical points. If there are fewer than two critical points, return [-1, -1].

Input & Output

Example 1 — Basic Case with Multiple Critical Points
$ Input: head = [3,1]
Output: [-1,-1]
💡 Note: There are no critical points in [3,1] since we need at least 3 nodes to have a node with both previous and next nodes.
Example 2 — Sufficient Critical Points
$ Input: head = [5,3,1,2,5,1,2]
Output: [1,4]
💡 Note: Critical points are at indices 1 (local maxima: 3>5 and 3>1), 2 (local minima: 1<3 and 1<2), 4 (local maxima: 5>2 and 5>1), and 5 (local minima: 1<5 and 1<2). Min distance is 1 (between indices 1 and 2, or 4 and 5), max distance is 4 (between indices 1 and 5).
Example 3 — Only Two Critical Points
$ Input: head = [1,3,2,2,3,2,2,2,7]
Output: [3,3]
💡 Note: Critical points are at indices 1 (local maxima: 3>1 and 3>2) and 4 (local maxima: 3>2 and 3>2). Distance between them is 3, so both min and max distances are 3.

Constraints

  • The number of nodes in the list is in the range [2, 105]
  • 1 ≤ Node.val ≤ 105

Visualization

Tap to expand
Min/Max Distance Between Critical Points INPUT Linked List: head = [3,1] 3 1 null Critical Point Definition: Local Maxima: node > prev AND node > next Local Minima: node < prev AND node < next Analysis: Node 3: No prev node Node 1: No next node No critical points possible! head = [3, 1] ALGORITHM STEPS 1 Initialize Variables firstCP, lastCP, prevCP = null minDist = MAX, position = 0 2 Single Pass Traversal Check each node for critical point condition 3 Track Critical Points Update minDist between consecutive critical points 4 Calculate Result If < 2 critical points: return [-1, -1] For head = [3,1]: - Only 2 nodes in list - Neither can be critical - Critical points found: 0 FINAL RESULT Output Array: [-1, -1] Why [-1, -1]? List has only 2 nodes. Critical points need both prev AND next nodes. Complexity: Time: O(n) - single pass Space: O(1) - constant OK - Optimized! Key Insight: A critical point requires BOTH a previous and next node. With only 2 nodes [3,1], neither node qualifies: node 3 has no previous, node 1 has no next. The optimized single-pass approach tracks first/last critical points for maxDistance and consecutive points for minDistance, returning [-1,-1] when fewer than 2 found. TutorialsPoint - Find the Minimum and Maximum Number of Nodes Between Critical Points | Optimized - Single Pass
Asked in
Amazon 15 Microsoft 12 Google 8
28.5K Views
Medium Frequency
~15 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen