Program to check whether given list is in valid state or not in Python

Sometimes we need to check if a list can be completely partitioned into valid groups. This problem involves grouping numbers using specific rules to determine if the entire list is in a "valid state".

Problem Definition

Given a list of numbers, check if every number can be grouped using one of these rules:

  • Contiguous pairs: (a, a) − two identical numbers

  • Identical triplets: (a, a, a) − three identical numbers

  • Consecutive triplets: (a, a+1, a+2) − three consecutive numbers

Example

For nums = [7, 7, 3, 4, 5], we can group [7, 7] as a pair and [3, 4, 5] as consecutive triplets, so the result is True.

Algorithm Approach

We use dynamic programming where dp[i] represents whether the first i elements can be validly grouped ?

class Solution:
    def solve(self, nums):
        n = len(nums)
        # dp[i] = True if first i elements can be validly grouped
        dp = [True] + [False] * n
        
        for i in range(2, n + 1):
            # Check if we can form a pair with last 2 elements
            if i >= 2 and dp[i - 2]:
                if nums[i - 1] == nums[i - 2]:
                    dp[i] = True
            
            # Check if we can form a triplet with last 3 elements
            if i >= 3 and dp[i - 3]:
                # Identical triplet or consecutive triplet
                if (nums[i - 1] == nums[i - 2] == nums[i - 3]) or \
                   (nums[i - 1] == nums[i - 2] + 1 == nums[i - 3] + 2):
                    dp[i] = True
        
        return dp[n]

# Test the solution
ob = Solution()
nums = [8, 8, 4, 5, 6]
result = ob.solve(nums)
print(f"Input: {nums}")
print(f"Output: {result}")
Input: [8, 8, 4, 5, 6]
Output: True

How It Works

The algorithm builds up the solution incrementally:

  1. Base case: dp[0] = True (empty list is valid)

  2. For each position i: Check if we can extend a valid grouping

  3. Pair check: If last 2 elements are identical and previous state was valid

  4. Triplet check: If last 3 elements form valid triplet and previous state was valid

Additional Examples

ob = Solution()

# Test case 1: Consecutive triplets
test1 = [1, 2, 3]
print(f"Test 1 - {test1}: {ob.solve(test1)}")

# Test case 2: Mixed groups
test2 = [5, 5, 1, 2, 3, 4, 4]
print(f"Test 2 - {test2}: {ob.solve(test2)}")

# Test case 3: Invalid grouping
test3 = [1, 2, 4]
print(f"Test 3 - {test3}: {ob.solve(test3)}")
Test 1 - [1, 2, 3]: True
Test 2 - [5, 5, 1, 2, 3, 4, 4]: True
Test 3 - [1, 2, 4]: False

Time and Space Complexity

  • Time Complexity: O(n) − single pass through the list

  • Space Complexity: O(n) − for the dp array

Conclusion

This dynamic programming solution efficiently checks if a list can be partitioned into valid groups by building up solutions incrementally. The algorithm considers both pair and triplet formations at each step to determine the final validity.

Updated on: 2026-03-25T11:47:14+05:30

460 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements