> For the complete documentation index, see [llms.txt](https://coding-9.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coding-9.gitbook.io/untitled/dynamic-programming/mcm-dp-or-partition-dp/2369.-check-if-there-is-a-valid-partition-for-the-array.md).

# 2369. Check if There is a Valid Partition For The Array

## Problem Statement

<br>

You are given a **0-indexed** integer array `nums`. You have to partition the array into one or more **contiguous** subarrays.

We call a partition of the array **valid** if each of the obtained subarrays satisfies **one** of the following conditions:

1. The subarray consists of **exactly** `2` equal elements. For example, the subarray `[2,2]` is good.
2. The subarray consists of **exactly** `3` equal elements. For example, the subarray `[4,4,4]` is good.
3. The subarray consists of **exactly** `3` consecutive increasing elements, that is, the difference between adjacent elements is `1`. For example, the subarray `[3,4,5]` is good, but the subarray `[1,3,5]` is not.

Return `true` *if the array has **at least** one valid partition*. Otherwise, return `false`.

&#x20;

**Example 1:**

<pre><code><strong>Input: nums = [4,4,4,5,6]
</strong><strong>Output: true
</strong><strong>Explanation: The array can be partitioned into the subarrays [4,4] and [4,5,6].
</strong>This partition is valid, so we return true.
</code></pre>

**Example 2:**

<pre><code><strong>Input: nums = [1,1,1,2]
</strong><strong>Output: false
</strong><strong>Explanation: There is no valid partition for this array.
</strong></code></pre>

&#x20;

**Constraints:**

* `2 <= nums.length <= 105`
* `1 <= nums[i] <= 106`<br>

## Intuition

```
Just input the three given conditions in recursion and memoize
```

### Links

<https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/description/>

### Video Links

### Approach 1:

```
Memoization
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    bool find_ans(vector<int>& nums, int index, vector<int> &dp){
        if(index == nums.size())
            return true;

        if(dp[index] != -1)
            return dp[index];

        if(index+1< nums.size() and nums[index] == nums[index+1]){
            if( find_ans(nums, index+2, dp) )
                return dp[index] = true;

            if(index+2< nums.size() and nums[index] == nums[index+2])
                if( find_ans(nums, index+3, dp) )
                    return dp[index] = true;
        }

        if(index+2 < nums.size() and nums[index] == nums[index+1]-1 and nums[index] == nums[index+2]-2){
            if( find_ans(nums, index+3, dp) )
                return dp[index] = true;
        }

        return dp[index] = false;
    }

    bool validPartition(vector<int>& nums) {
        int n = nums.size();
        vector<int> dp(n, -1);

        return find_ans(nums, 0, dp);
    }
};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###
