
- 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 elements less than or equal to a given number in a given subarray in C++
You are given a number and subarray lower and upper bound indexes. You need to count a number of elements that are less than or equal to the given number. Let's see an example.
Input
arr = [1, 2, 3, 4, 5, 6, 7, 8] k = 4 lower = 0 upper = 5
Output
4
There are 4 elements between the index 0 and 5 that are less than or equal to 4.
Algorithm
Initialise the array, number, and subarray indexes.
Initialise the count to 0.
Write a loop that iterates from the lower index of the subarray to the upper index of the subarray.
If the current element is less than or equal to the given number, then increment the count.
Return the count.
Implementation
Following is the implementation of the above algorithm in C++
#include <bits/stdc++.h> using namespace std; int getElementsCount(int arr[], int n, int lower, int upper, int k) { if (lower < 0 || upper >= n || lower > upper) { return 0; } int count = 0; for (int i = lower; i <= upper; i++) { if (arr[i] <= k) { count += 1; } } return count; } int main() { int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; int n = 8, k = 4; cout << getElementsCount(arr, n, 0, 3, k) << endl; cout << getElementsCount(arr, n, 4, 7, k) << endl; return 0; }
Output
If you run the above code, then you will get the following result.
4 0
- Related Articles
- Find Elements Greater Than a Given Number In a Subarray in Java
- Maximum sum subarray having sum less than or equal to given sums in C++
- Find Largest Special Prime which is less than or equal to a given number in C++
- Count elements less than or equal to a given value in a sorted rotated array in C++
- Java Program to display a prime number less than the given number
- Find Number of Array Elements Smaller than a Given Number in Java
- JavaScript Program to Check whether all the rotations of a given number are greater than or equal to the given number or not
- Mask an array where less than or equal to a given value in Numpy
- Mask array elements greater than or equal to a given value in Numpy
- How to get the largest integer less than or equal to a number in JavaScript?
- Nearest prime less than given number n C++
- Check which element in a masked array is less than or equal to a given value in Numpy
- Mask array elements less than a given value in Numpy
- Find maximum number of elements such that their absolute difference is less than or equal to 1 in C++
- Number of indexes with equal elements in given range in C++

Advertisements