
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold in C++
Suppose we have an array of integers arr and two integers k and threshold. We have to find the number of sub-arrays of size k and average greater than or equal to the threshold. So if the input is like: [2,2,2,2,5,5,5,8] and k = 3 and threshold = 4, then the output will be 3. Because the subarrays [2,5,5], [5,5,5] and [5,5,8] has the averages 4, 5 and 6 respectively.
To solve this, we will follow these steps −
sum := 0, div := k and n := number of elements in array
set sum := sum of all elements of arr
ret := 0
for i := 0 and j in range k to n – 1, increase both i and j by 1
if sum / div >= threshold, then increase res by 1
decrease sum by arr[i]
increase sum by arr[j]
if sum / div >= threshold, then increase ret by 1
return ret.
Example (C++)
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: int numOfSubarrays(vector<int>& arr, int k, int threshold) { double sum = 0; double div = k; int n = arr.size(); for(int i = 0; i < k; i++){ sum += arr[i]; } int ret = 0; for(int i = 0, j = k; j < n; i ++, j++){ if(sum / div >= threshold ){ ret++; } sum -= arr[i]; sum += arr[j]; } if(sum / div >= threshold ){ ret++; } return ret; } }; main(){ vector<int> v = {2,2,2,2,5,5,5,8}; Solution ob; cout << (ob.numOfSubarrays(v, 3, 4)); }
Input
[2,2,2,2,5,5,5,8] 3 4
Output
3
- Related Articles
- Python – Average of digit greater than K
- Count the number of sub-arrays such that the average of elements present in the subarray is greater than that not present in the sub-array in C++
- Count sub-arrays which have elements less than or equal to X in C++
- Getting equal or greater than number from the list of numbers in JavaScript
- Complete the following statements:The probability of an event is greater than or equal to …………. and less than or equal to ………..
- Adding elements of an array until every element becomes greater than or equal to k in C++.
- Python - Number of values greater than K in list
- C++ program to Adding elements of an array until every element becomes greater than or equal to k
- Program to find number of K-Length sublists whose average is greater or same as target in python
- Maximum Side Length of a Square with Sum Less than or Equal to Threshold in C++
- Program to split lists into strictly increasing sublists of size greater than k in Python
- Maximize the size of array by deleting exactly k sub-arrays to make array prime in C++
- Largest number smaller than or equal to N divisible by K in C++
- How to get the smallest integer greater than or equal to a number in JavaScript?
- Count the number of words having sum of ASCII values less than and greater than k in C++
