Python Program to find out the number of sets greater than a given value

Suppose we have an array containing several integer numbers. We need to find all contiguous subarrays, replace each subarray with its maximum element, and count how many of these maximum values are greater than a given number k.

So, if the input is like input_array = [5, 6, 7, 8], k = 7, then the output will be 4.

Understanding the Problem

The contiguous subarrays from the given input array are:

{5}, {6}, {7}, {8}, {5, 6}, {6, 7}, {7, 8}, {5, 6, 7}, {6, 7, 8}, {5, 6, 7, 8}

If we replace each subarray with its maximum element, the values become:

{5}, {6}, {7}, {8}, {6}, {7}, {8}, {7}, {8}, {8}

There are 4 values greater than 7: {8}, {8}, {8}, {8}.

Algorithm

To solve this efficiently, we use the following approach:

  • Calculate the total number of subarrays: n * (n + 1) / 2
  • Count subarrays where maximum element is ? k
  • Subtract the count from total to get subarrays with maximum > k

Implementation

def solve(input_array, k):
    count = 0
    consecutive = 0
    
    for x in input_array:
        if x > k:
            consecutive = 0
        else:
            consecutive += 1
            count += consecutive
    
    total_subarrays = len(input_array) * (len(input_array) + 1) // 2
    return total_subarrays - count

# Test the function
result = solve([5, 6, 7, 8], 7)
print(f"Number of subarrays with maximum > 7: {result}")
Number of subarrays with maximum > 7: 4

How It Works

The algorithm counts subarrays where the maximum element is ? k by tracking consecutive elements that satisfy this condition. When we encounter an element > k, it resets the consecutive count because any subarray containing this element will have a maximum > k.

def solve_with_explanation(input_array, k):
    print(f"Input array: {input_array}, k = {k}")
    
    total_subarrays = len(input_array) * (len(input_array) + 1) // 2
    print(f"Total subarrays: {total_subarrays}")
    
    count = 0
    consecutive = 0
    
    for i, x in enumerate(input_array):
        if x > k:
            consecutive = 0
            print(f"Element {x} > {k}, reset consecutive to 0")
        else:
            consecutive += 1
            count += consecutive
            print(f"Element {x} ? {k}, consecutive = {consecutive}, count = {count}")
    
    result = total_subarrays - count
    print(f"Subarrays with max ? {k}: {count}")
    print(f"Subarrays with max > {k}: {result}")
    return result

solve_with_explanation([5, 6, 7, 8], 7)
Input array: [5, 6, 7, 8], k = 7
Total subarrays: 10
Element 5 ? 7, consecutive = 1, count = 1
Element 6 ? 7, consecutive = 2, count = 3
Element 7 ? 7, consecutive = 3, count = 6
Element 8 > 7, reset consecutive to 0
Subarrays with max ? 7: 6
Subarrays with max > 7: 4

Another Example

# Test with different input
test_cases = [
    ([1, 2, 3, 4, 5], 3),
    ([8, 2, 6, 1], 5),
    ([10, 20, 30], 15)
]

for arr, k in test_cases:
    result = solve(arr, k)
    print(f"Array: {arr}, k = {k} ? Result: {result}")
Array: [1, 2, 3, 4, 5], k = 3 ? Result: 6
Array: [8, 2, 6, 1], k = 5 ? Result: 4
Array: [10, 20, 30], k = 15 ? Result: 5

Conclusion

This algorithm efficiently counts subarrays with maximum element greater than k using a mathematical approach. It runs in O(n) time complexity by avoiding the need to explicitly generate all subarrays.

Updated on: 2026-03-25T20:30:00+05:30

389 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements