Skip to main content

Decrease Elements To Make Array Zigzag - Solution & Explanation

MediumArrayGreedy11 min readAsked at: Google
Practice this problem

Problem Statement

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:

  • Every even-indexed element is greater than adjacent elements, ie. A[0] > A[1] < A[2] > A[3] < A[4] > ...
  • OR, every odd-indexed element is greater than adjacent elements, ie. 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 <= 1000
  • 1 <= nums[i] <= 1000

Approach Overview

Problem 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 1: Pattern-Based Adjustment

This approach considers two patterns for the zigzag sequence: even-indexed elements being greater than their neighbors and odd-indexed elements being greater than their neighbors. We calculate the moves necessary to transition to both patterns and return the minimum of these values as the result.

In this solution, we iterate over the array twice. Once, considering adjustments for the pattern where even-indexed elements are greater, and once for the pattern where odd-indexed elements are greater. We calculate the moves required to make each element less than its neighbors if it's in the non-leading position of the pattern. We return the minimum of the two counts of moves.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of elements in the array. We perform a constant-time operation for each element.
Space Complexity: O(1), since we use a fixed amount of extra memory.

Try this approach in the editor →

Approach 2: Enumeration + Greedy

We can separately enumerate the even and odd positions as the elements "smaller than adjacent elements", and then calculate the required number of operations. The minimum of the two is taken.

The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

C#

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Pattern-Based Adjustment

Time Complexity: O(n), where n is the number of elements in the array. We perform a constant-time operation for each element.
Space Complexity: O(1), since we use a fixed amount of extra memory.

Enumeration + Greedy

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Neighbor AdjustmentO(n)O(1)Good for reasoning about the zigzag condition and validating both possible patterns.
Pattern-Based Greedy AdjustmentO(n)O(1)Best choice for interviews and production. Computes minimal decrements in two linear scans.

Video Solution

Decrease Elements To Make Array Zigzag (Leetcode 1144)Coding Interviews1,824 views views

Watch 4 more video solutions →

Frequently Asked Questions

Is Decrease Elements To Make Array Zigzag easy or hard?
Decrease Elements To Make Array Zigzag is classified as Medium difficulty. The implementation is straightforward once you recognize that two zigzag patterns must be evaluated. The main challenge is identifying the greedy insight that only valley positions need to be decreased.
Decrease Elements To Make Array Zigzag Python/Java solution
In Python or Java, implement a loop that calculates the decrement cost for each index based on the minimum of its neighbors. Run the calculation twice: once assuming even indices are valleys and once assuming odd indices are valleys. Return the minimum of the two totals. The implementation uses simple array access and integer arithmetic.
How to solve Decrease Elements To Make Array Zigzag in O(n)?
Compute the cost of making even indices valleys and the cost of making odd indices valleys. For each index, check the minimum of its adjacent elements and determine how much the current value must be decreased to be strictly smaller. Accumulate the required decrements and return the smaller total. This requires two linear passes, giving O(n) time and constant extra space.
What is the best approach for Decrease Elements To Make Array Zigzag?
The best approach is a greedy pattern-based adjustment. Evaluate two possible zigzag configurations: even indices as valleys or odd indices as valleys. For each index, compute the number of decrements needed to make it smaller than its neighbors. Summing these costs for each pattern and returning the minimum gives an O(n) time and O(1) space solution.
Is Decrease Elements To Make Array Zigzag asked at Google/Amazon/Meta?
This problem reflects common interview themes used by companies like Google, Amazon, and Meta: greedy reasoning and local constraint optimization on arrays. While the exact question may vary, similar zigzag or alternating pattern problems frequently appear in coding interviews.
What data structure is used in Decrease Elements To Make Array Zigzag?
The solution primarily uses a simple array traversal with constant extra variables. No advanced data structures are required. The logic relies on comparing each element with its immediate neighbors and applying a greedy adjustment strategy.
What is the time complexity of Decrease Elements To Make Array Zigzag?
The optimal solution runs in O(n) time because the array is scanned twice—once for each possible zigzag pattern. Each step performs constant-time comparisons with neighboring elements. The space complexity is O(1) since only counters and temporary values are used.

Ready to solve this problem?

Practice Decrease Elements To Make Array Zigzag with our built-in code editor and test cases.

Practice on FleetCode