DEV Community

Nithya Dharshini official
Nithya Dharshini official

Posted on

Understanding Array Splitting Problems Through a Simple Cost Model

LeetCode 3010 – Divide an Array Into Subarrays With Minimum Cost I

When I first read array splitting problems, I often feel stuck — not because of syntax or logic, but because I don’t clearly understand how the array is being split and why the cost is defined the way it is.

This article explains how I learned to see the split instead of guessing it.

Problem Statement (Simplified)

You are given an integer array nums of length n.

  • You must divide the array into 3 disjoint contiguous subarrays
  • The cost of a subarray is the value of its first element
  • Return the minimum possible sum of the costs
nums = [1, 2, 3, 12]

Enter fullscreen mode Exit fullscreen mode

My Initial Confusion

When I read this problem, one questions blocked me immediately:
How exactly do I split the array into 3 parts?

I realized that many array-splitting problems don’t explain the mechanics — they expect you to visualize it.

Key Insight: What “Splitting” Really Means

To divide an array into 3 contiguous subarrays, we must choose 2 split points.

[ a | b | c ]
Enter fullscreen mode Exit fullscreen mode

Then:

  1. a is the start of the first subarray
  2. b is the start of the second subarray
  3. c is the start of the third subarray

👉 The cost depends only on these three starting positions

Reframing the Problem

Instead of thinking:

“How do I split the array?”

Think:

“Which 3 indices will be the first elements of the 3 subarrays?”

What I Tried (Brute Force, but Clear)

#include <iostream>
#include <vector>
#include <climits>
using namespace std;

int main() {
    vector<int> v = {1, 2, 3, 12};
    int mini = INT_MAX;

    for (int i = 1; i < v.size(); ++i) {
        for (int j = i + 1; j < v.size(); ++j) {
            int cost = v[0] + v[i] + v[j];
            mini = min(mini, cost);
        }
    }

    cout << mini;
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

What This Code Is Actually Doing

Variable Meaning
v[0] Start of first subarray
v[i] Start of second subarray
v[j] Start of third subarray
Subarrays Cost Calculation Total Cost
[1] [2] [3,12] 1 + 2 + 3 6
[1] [2,3] [12] 1 + 2 + 12 15
[1,2] [3] [12] 1 + 3 + 12 16

Complexity

Time: O(n²)
Space: O(1)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)