
- 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
Count elements less than or equal to a given value in a sorted rotated array in C++
We are given with an array of integers. The array is a sorted rotated array. The goal is to find the number of elements in the array that are equal to or less than the given number K.
Approach is to traverse the whole array and count such elements which are either less or equal to K.
Input
Arr[]= { 1,2,3,4,9,8,10 } K=4
Output
Elements less than or equal to 4 : 4
Explanation − Elements <=4 are 1,2,3,4 Count=4
Input
Arr[]= { 5,3,6,1,8,100,12,31 } K=3
Output
Elements less than or equal to 3: 2
Explanation − Elements <=3 are 1,3 Count=2
Approach used in the below program is as follows
The integer array Arr[] is used to store the integers, K to denote a number.
Integer ‘n’ stores the length of the array.
Variable count is used to store the count of numbers less or equal to K.
Traverse the array once starting from the first element( index=0 ).
If current element <=K increment count.
Count contains the desired result.
Display the result.
Example
#include <iostream> using namespace std; int main(){ int Arr[]= { 4,5,8,1,3,7,10,9,11 }; int k=7; int n=sizeof(Arr)/sizeof(Arr[0]); int count=0; for(int i=0;i<n;i++) if(Arr[i]<=k) count++; std::cout<<"Elements less than or equal to "<<k<<" in given sorted rotated array : "<<count; return 0; }
Output
Elements less than or equal to 7 in given sorted rotated array : 5
- Related Articles
- 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
- Count elements smaller than or equal to x in a sorted matrix in C++
- Mask array elements less than a given value in Numpy
- Check which element in a masked array is less than or equal to a given value in Numpy
- Number of elements less than or equal to a given number in a given subarray in C++
- Count of smaller or equal elements in the sorted array in C++
- Count sub-arrays which have elements less than or equal to X in C++
- Count pairs in a sorted array whose sum is less than x in C++
- Count pairs in a sorted array whose product is less than k in C++
- How to find numbers in an array that are greater than, less than, or equal to a value in java?
- Find the Rotation Count in Rotated Sorted array in C++
- Mask array elements equal to a given value in Numpy
- Mask array elements not equal to a given value in Numpy
- Check which element in a masked array is greater than or equal to a given value in NumPy

Advertisements