Watch 8 video solutions for Decrease Elements To Make Array Zigzag, a medium level problem involving Array, Greedy. This walkthrough by Coding Interviews has 1,795 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given an array nums of integers, a move consists of choosing any element and decreasing it by 1.
An array A is a zigzag array if either:
A[0] > A[1] < A[2] > A[3] < A[4] > ...A[0] < A[1] > A[2] < A[3] > A[4] < ...Return the minimum number of moves to transform the given array nums into a zigzag array.
Example 1:
Input: nums = [1,2,3] Output: 2 Explanation: We can decrease 2 to 0 or 3 to 1.
Example 2:
Input: nums = [9,6,1,6,2] Output: 4
Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 1000Problem Overview: You’re given an integer array and can only decrease values. The goal is to transform the array into a zigzag pattern with the minimum number of decrement operations. A zigzag array means either nums[0] < nums[1] > nums[2] < nums[3]... or nums[0] > nums[1] < nums[2] > nums[3].... The task is to compute the minimum total decrements required to achieve either pattern.
Approach 1: Brute Force Neighbor Adjustment (O(n), O(1))
Check both valid zigzag configurations separately. For one pass, enforce that even indices are valleys (nums[i] < nums[i-1] and nums[i] < nums[i+1]), and for the other pass enforce the same rule for odd indices. For each index, compare the value with its neighbors and calculate how much it must be decreased to become strictly smaller than the minimum neighbor. Accumulate the required decrements without modifying the original array. This works because decreasing one element never breaks the constraint for other indices when evaluated independently.
Approach 2: Pattern-Based Greedy Adjustment (O(n), O(1))
This is the optimal greedy strategy. Instead of modifying the array, compute the cost of making each index the "valley" of the zigzag pattern. For index i, find the smallest neighbor among nums[i-1] and nums[i+1]. If nums[i] is not already smaller, calculate the decrements required to make it minNeighbor - 1. Sum these costs for all indices of the same parity (even or odd). Since a valid zigzag can start with either a peak or valley, run this calculation twice: once assuming even indices are valleys and once assuming odd indices are valleys. Return the smaller cost. The algorithm scans the array once per pattern and uses only constant extra memory, making it efficient for large inputs.
The core insight: only the valley positions need adjustment. Peaks automatically satisfy the constraint if their neighboring valleys are smaller. By computing the minimal decrement needed relative to neighbors, the algorithm avoids unnecessary changes and keeps the solution linear.
This problem is a good exercise in recognizing local constraints in array problems and applying a simple greedy decision at each index.
Recommended for interviews: The pattern-based greedy approach is what interviewers expect. It shows you can reason about local constraints and reduce the problem to two deterministic passes. Explaining both zigzag patterns first demonstrates complete understanding, then implementing the O(n) greedy calculation shows practical problem-solving skill.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Neighbor Adjustment | O(n) | O(1) | Good for reasoning about the zigzag condition and validating both possible patterns. |
| Pattern-Based Greedy Adjustment | O(n) | O(1) | Best choice for interviews and production. Computes minimal decrements in two linear scans. |