- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

Suppose, we have an array containing several integer numbers. We find out all the contiguous subarrays from the given array of numbers and put it into another list. Now, we replace each subarray with the maximum element in that subarray. There is also a number k given to us, and we have to find out how many subarrays are now greater than the given number.

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

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 update the subarrays with the maximum element in them, the subarrays become −

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

There are 4 sets where the element is greater than 7.

To solve this, we will follow these steps −

- count := 0
- consecutive := 0
- for each x in input_array, do
- if x > k, then
- consecutive := 0

- otherwise,
- consecutive := consecutive + 1
- count := count + consecutive

- if x > k, then

return size of input_array * floor value of ((size of input_array + 1) / 2) - count

## Example

Let us see the following implementation to get better understanding −

def solve(input_array, k): count = 0 consecutive = 0 for x in input_array: if x > k: consecutive = 0 else: consecutive += 1 count += consecutive return len(input_array) * (len(input_array) + 1) // 2 - count print(solve([5, 6, 7, 8], 7))

## Input

[5, 6, 7, 8], 7

## Output

4

- Related Articles
- Program to find out the number of consecutive elements in a matrix whose gcd is greater than 1 in Python
- Program to find out the value of a given equation in Python
- Program to find lowest sum of pairs greater than given target in Python
- Python - Find words greater than given length
- Python program to check if all the values in a list that are greater than a given value
- Python program to count the number of vowels using sets in a given string
- Program to check if all the values in a list that are greater than a given value in Python
- Program to find out the number of special numbers in a given range in Python
- Find the Next perfect square greater than a given number in C++
- C program to find out the maximum value of AND, OR, and XOR operations that are less than a given value
- Number of nodes greater than a given value in n-ary tree in C++
- Program to find out number of distinct substrings in a given string in python
- How to find the smallest number greater than x in Python?
- Python Program to find Duplicate sets in list of sets
- Program to find out the largest sum value of a BST in a given binary tree in Python