Program to find number of strictly increasing colorful candle sequences are there in Python

This problem asks us to find the number of strictly increasing colorful candle sequences. A sequence is strictly increasing based on candle heights, and colorful if it contains at least one candle of each color from 1 to k.

The solution uses the inclusion-exclusion principle combined with a Binary Indexed Tree (BIT) to efficiently count valid subsequences. For each subset of colors, we count increasing subsequences, then apply inclusion-exclusion to ensure all k colors are present.

Algorithm Explanation

The approach works as follows ?

  • Use bit manipulation to represent subsets of colors (2^k possibilities)
  • For each subset, count strictly increasing subsequences using BIT
  • Apply inclusion-exclusion: add counts for subsets with even complement size, subtract for odd complement size
  • BIT efficiently maintains counts of subsequences ending at each height

Example

Let us see the implementation with proper explanation ?

def solve(k, h, c):
    def read(T, i):
        s = 0
        while i > 0:
            s += T[i]
            s %= 1000000007
            i -= (i & -i)
        return s

    def update(T, i, v):
        while i <= 50010:
            T[i] += v
            T[i] %= 1000000007
            i += (i & -i)
        return v

    def number_of_bits(b):
        c = 0
        while b:
            b &= b - 1
            c += 1
        return c

    L = 2 ** k
    R = 0
    N = len(h)

    for i in range(L):
        T = [0 for _ in range(50010)]
        t = 0

        for j in range(N):
            if (i >> (c[j] - 1)) & 1:
                t += update(T, h[j], read(T, h[j] - 1) + 1)
                t %= 1000000007

        if number_of_bits(i) % 2 == k % 2:
            R += t
            R %= 1000000007
        else:
            R += 1000000007 - t
            R %= 1000000007
    return R

# Test the function
k = 3
heights = [1, 3, 2, 4]
colors = [1, 2, 2, 3]

result = solve(k, heights, colors)
print(f"Number of colorful sequences: {result}")
Number of colorful sequences: 2

How It Works

For the input k=3, heights=[1,3,2,4], colors=[1,2,2,3] ?

  • We need sequences containing all colors 1, 2, and 3
  • Valid sequences are strictly increasing by height: [1,3,4] and [1,2,4]
  • Sequence [1,3,4] uses candles at positions (0,1,3) with colors (1,2,3)
  • Sequence [1,2,4] uses candles at positions (0,2,3) with colors (1,2,3)

Key Components

  • Binary Indexed Tree: Efficiently counts subsequences ending at each height
  • Inclusion-Exclusion: Ensures exactly k colors are present, not just a subset
  • Bit Manipulation: Represents color subsets for systematic enumeration

Conclusion

This solution efficiently finds colorful increasing sequences using inclusion-exclusion principle with Binary Indexed Tree optimization. The time complexity is O(k × 2^k × n × log(max_height)) which handles the constraint requirements effectively.

Updated on: 2026-03-26T18:14:28+05:30

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements